Generic stored procedure to merge tables in a single stored procedure to read all the tables from one table to particular table [closed]

I have created 20 tables .10 tables(target) for full load from AWS athena using streamsets and remaining 10 tables(source tables) for incremental from AWS athena using streamsets to postgresql (dbeaver). Now I want to merge them one source table to one target table.

I used this query to merge the tables in postgresql.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE OR REPLACE PROCEDURE merge_all_tables()
LANGUAGE plpgsql
AS $$
BEGIN
-- Merge source_table_a into target_table_a1
MERGE INTO target_table_a1 AS t
USING source_table_a AS s
ON t.id = s.id
WHEN NOT MATCHED THEN
INSERT (id, name, balance)
VALUES (s.id, s.name, s.balance);
-- Merge source_table_b into target_table_b1
MERGE INTO target_table_b1 AS t
USING source_table_b AS s
ON t.id = s.id
WHEN NOT MATCHED THEN
INSERT (id, name, balance)
VALUES (s.id, s.name, s.balance);
-- Repeat MERGE for other source and target table pairs (D to D1, E to E1, etc.)
-- Merge source_table_d into target_table_d1
MERGE INTO target_table_d1 AS t
USING source_table_d AS s
ON t.id = s.id
WHEN NOT MATCHED THEN
INSERT (id, name, balance)
VALUES (s.id, s.name, s.balance);
END;
$$;
</code>
<code>CREATE OR REPLACE PROCEDURE merge_all_tables() LANGUAGE plpgsql AS $$ BEGIN -- Merge source_table_a into target_table_a1 MERGE INTO target_table_a1 AS t USING source_table_a AS s ON t.id = s.id WHEN NOT MATCHED THEN INSERT (id, name, balance) VALUES (s.id, s.name, s.balance); -- Merge source_table_b into target_table_b1 MERGE INTO target_table_b1 AS t USING source_table_b AS s ON t.id = s.id WHEN NOT MATCHED THEN INSERT (id, name, balance) VALUES (s.id, s.name, s.balance); -- Repeat MERGE for other source and target table pairs (D to D1, E to E1, etc.) -- Merge source_table_d into target_table_d1 MERGE INTO target_table_d1 AS t USING source_table_d AS s ON t.id = s.id WHEN NOT MATCHED THEN INSERT (id, name, balance) VALUES (s.id, s.name, s.balance); END; $$; </code>
CREATE OR REPLACE PROCEDURE merge_all_tables()
LANGUAGE plpgsql
AS $$
BEGIN
    -- Merge source_table_a into target_table_a1
    MERGE INTO target_table_a1 AS t
    USING source_table_a AS s
    ON t.id = s.id
    WHEN NOT MATCHED THEN
        INSERT (id, name, balance)
        VALUES (s.id, s.name, s.balance);

    -- Merge source_table_b into target_table_b1
    MERGE INTO target_table_b1 AS t
    USING source_table_b AS s
    ON t.id = s.id 
    WHEN NOT MATCHED THEN
        INSERT (id, name, balance)
        VALUES (s.id, s.name, s.balance);

  

    -- Repeat MERGE for other source and target table pairs (D to D1, E to E1, etc.)

    -- Merge source_table_d into target_table_d1
    MERGE INTO target_table_d1 AS t
    USING source_table_d AS s
    ON t.id = s.id
       WHEN NOT MATCHED THEN
        INSERT (id, name, balance)
        VALUES (s.id, s.name, s.balance);

    
END;
$$;

THIS IS THE HARD CODING WHICH I HAVE ENCOUNTERED. BUTM I NEED THE QUERY WHICH READS AND INSERTS INTO THE TARGET FROM INFORMATUON SCHEMA.

I got this

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>CREATE OR REPLACE PROCEDURE merge_multiple_tables()
LANGUAGE plpgsql
AS $$
DECLARE
-- Array of source-target-primary key pairs
source_tables TEXT[] := ARRAY['source_table_1', 'source_table_2', 'source_table_3', 'source_table_4', 'source_table_5'];
target_tables TEXT[] := ARRAY['target_table_1', 'target_table_2', 'target_table_3', 'target_table_4', 'target_table_5'];
primary_keys TEXT[] := ARRAY['id_1', 'id_2', 'order_id', 'emp_id', 'item_code'];
column_list TEXT;
insert_values TEXT;
i INT;
BEGIN
-- Loop through each source-target-primary key pair
FOR i IN 1..array_length(source_tables, 1)
LOOP
-- Dynamically retrieve column names for the target table
SELECT string_agg(quote_ident(column_name), ', ')
INTO column_list
FROM information_schema.columns
WHERE lower(table_name) = lower(target_tables[i]); -- Convert table names to lowercase to avoid case issues
-- Dynamically retrieve values for the source table columns for the insert clause
SELECT string_agg('source.' || quote_ident(column_name), ', ')
INTO insert_values
FROM information_schema.columns
WHERE lower(table_name) = lower(target_tables[i]); -- Same column names as the target table
-- Dynamically build and execute the MERGE statement
EXECUTE '
MERGE INTO ' || quote_ident(target_tables[i]) || ' AS target
USING ' || quote_ident(source_tables[i]) || ' AS source
ON target.' || quote_ident(primary_keys[i]) || ' = source.' || quote_ident(primary_keys[i]) || '
WHEN MATCHED THEN
UPDATE SET
' || (
SELECT string_agg('target.' || quote_ident(column_name) || ' = source.' || quote_ident(column_name), ', ')
FROM information_schema.columns
WHERE lower(table_name) = lower(target_tables[i])
) || '
WHEN NOT MATCHED THEN
INSERT (' || column_list || ')
VALUES (' || insert_values || ');
';
END LOOP;
END;
$$;
</code>
<code>CREATE OR REPLACE PROCEDURE merge_multiple_tables() LANGUAGE plpgsql AS $$ DECLARE -- Array of source-target-primary key pairs source_tables TEXT[] := ARRAY['source_table_1', 'source_table_2', 'source_table_3', 'source_table_4', 'source_table_5']; target_tables TEXT[] := ARRAY['target_table_1', 'target_table_2', 'target_table_3', 'target_table_4', 'target_table_5']; primary_keys TEXT[] := ARRAY['id_1', 'id_2', 'order_id', 'emp_id', 'item_code']; column_list TEXT; insert_values TEXT; i INT; BEGIN -- Loop through each source-target-primary key pair FOR i IN 1..array_length(source_tables, 1) LOOP -- Dynamically retrieve column names for the target table SELECT string_agg(quote_ident(column_name), ', ') INTO column_list FROM information_schema.columns WHERE lower(table_name) = lower(target_tables[i]); -- Convert table names to lowercase to avoid case issues -- Dynamically retrieve values for the source table columns for the insert clause SELECT string_agg('source.' || quote_ident(column_name), ', ') INTO insert_values FROM information_schema.columns WHERE lower(table_name) = lower(target_tables[i]); -- Same column names as the target table -- Dynamically build and execute the MERGE statement EXECUTE ' MERGE INTO ' || quote_ident(target_tables[i]) || ' AS target USING ' || quote_ident(source_tables[i]) || ' AS source ON target.' || quote_ident(primary_keys[i]) || ' = source.' || quote_ident(primary_keys[i]) || ' WHEN MATCHED THEN UPDATE SET ' || ( SELECT string_agg('target.' || quote_ident(column_name) || ' = source.' || quote_ident(column_name), ', ') FROM information_schema.columns WHERE lower(table_name) = lower(target_tables[i]) ) || ' WHEN NOT MATCHED THEN INSERT (' || column_list || ') VALUES (' || insert_values || '); '; END LOOP; END; $$; </code>
CREATE OR REPLACE PROCEDURE merge_multiple_tables()
LANGUAGE plpgsql
AS $$
DECLARE
    -- Array of source-target-primary key pairs
    source_tables TEXT[] := ARRAY['source_table_1', 'source_table_2', 'source_table_3', 'source_table_4', 'source_table_5'];
    target_tables TEXT[] := ARRAY['target_table_1', 'target_table_2', 'target_table_3', 'target_table_4', 'target_table_5'];
    primary_keys TEXT[] := ARRAY['id_1', 'id_2', 'order_id', 'emp_id', 'item_code'];
    
    column_list TEXT;
    insert_values TEXT;
    i INT;
BEGIN
    -- Loop through each source-target-primary key pair
    FOR i IN 1..array_length(source_tables, 1)
    LOOP
        -- Dynamically retrieve column names for the target table
        SELECT string_agg(quote_ident(column_name), ', ')
        INTO column_list
        FROM information_schema.columns
        WHERE lower(table_name) = lower(target_tables[i]); -- Convert table names to lowercase to avoid case issues

        -- Dynamically retrieve values for the source table columns for the insert clause
        SELECT string_agg('source.' || quote_ident(column_name), ', ')
        INTO insert_values
        FROM information_schema.columns
        WHERE lower(table_name) = lower(target_tables[i]); -- Same column names as the target table

        -- Dynamically build and execute the MERGE statement
        EXECUTE '
            MERGE INTO ' || quote_ident(target_tables[i]) || ' AS target
            USING ' || quote_ident(source_tables[i]) || ' AS source
            ON target.' || quote_ident(primary_keys[i]) || ' = source.' || quote_ident(primary_keys[i]) || '
            WHEN MATCHED THEN
                UPDATE SET
                    ' || (
                        SELECT string_agg('target.' || quote_ident(column_name) || ' = source.' || quote_ident(column_name), ', ')
                        FROM information_schema.columns
                        WHERE lower(table_name) = lower(target_tables[i])
                    ) || '
            WHEN NOT MATCHED THEN
                INSERT (' || column_list || ')
                VALUES (' || insert_values || ');
        ';
    END LOOP;
END;
$$;
  

while calling the function it is showing error at the target (like column is not found but everything was exact)

I need a stored procedure

New contributor

Diviya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

3

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật