Database

January 08, 2023

by Administrator

MERGE INTO target_table a
USING source_table b
ON (a.id=b.id) --join condition
    WHEN MATCHED THEN
        UPDATE SET col1 = value1, col2 = value2,...
        [WHERE <update_condition>]
        [DELETE WHERE <delete_condition>] 
    WHEN NOT MATCHED THEN
        INSERT (col1,col2,...)
        values(value1,value2,...)
        WHERE <insert_condition>;
MERGE INTO target_table a
USING source_table b
ON (a.p_id=b.id)
    WHEN MATCHED THEN
        UPDATE SET a.parent_name = b.name
        WHERE a.parent_name is null;
COMMIT;

The destination source can either be a table name or a script

 

MERGE INTO target_table a
USING (select id,parent_name from source_table) b
ON (a.p_id=b.id)
    WHEN MATCHED THEN
        UPDATE SET a.parent_name = b.name
        WHERE a.parent_name is null;
COMMIT;

 


Originally posted on medium


References

https://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF01606

https://oracle-base.com/articles/9i/merge-statement

https://www.oracletutorial.com/oracle-basics/oracle-merge/

 

 



Comments

Your email address will not be published. All fields are required

Comment*
Name *
Email *
DKYlcndJTZVmgwGk
May 15 2024, 03:08 AM

DMqvcUAfCHxP

FalIpbxmTY
May 15 2024, 08:18 AM

cJkAujGKV

wHJOlzukTURWx
May 27 2024, 03:52 PM

twsTYhzoERgXWDN

udjrmJAh
Jun 03 2024, 10:07 AM

rZVgxwBmiUAQj

uHCKJsPkLlefUzSD
Jun 15 2024, 01:32 AM

KDPAXoCHF