To use dynamic SQL column names in a Merge/Insert statement in Oracle, you can follow the below steps:
SELECT column_name
FROM all_tab_columns
WHERE table_name = 'table_name'
AND column_id > 3;
DECLARE
col_names VARCHAR2(200);
BEGIN
SELECT LISTAGG(column_name,',') WITHIN GROUP (ORDER BY column_id)
INTO col_names
FROM all_tab_columns
WHERE table_name = 'table_name'
AND column_id > 3;
END;
MERGE INTO table_name t
USING (
SELECT value1, value2, value3, ...
FROM source_table
) s
ON (t.key_column = s.key_column)
WHEN MATCHED THEN
UPDATE SET
t.column1 = s.column1,
t.column2 = s.column2,
...
t.columnN = s.columnN
WHEN NOT MATCHED THEN
INSERT (key_column, col_names)
VALUES (s.key_column, s.value1, s.value2, s.value3, ..., s.valueN);
Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss
Asked: 2023-06-03 04:18:02 +0000
Seen: 16 times
Last updated: Jun 03 '23
What is the process for transforming a CURL command into PL/SQL?
What is the process for designing a success message that shows the column value in APEX?
How can null columns be dynamically generated in PL/SQL?
How can operations be performed on the return value of a function in Oracle PLSQL?
How to open a new tab when using OWA_UTIL.REDIRECT_URL in PL/SQL?
How can the issue of receiving an empty return from Json parse in plsql be restated?
How can the closure of a sysrefcursor that is returned from a Plsql Stored Procedure be managed?
What is the time-consuming process of creating a CSV containing 200,000 lines using Oracle PL/SQL?
What is the method to monitor the execution of a Plsql stored procedure?