简体   繁体   中英

MERGE with multiple UPDATE statements

Can we achieve below scenario using single MERGE statement:

source table - table1 destination table- table2

when table1.id in table2.id then update table1 SET phone_number=123456

when table1.id not in table2.id then update table1 SET phone_number=555555

Note:- i am able to achieve the result using below query .

MERGE INTO table1 tbl1
USING table2 tbl2 
ON (tbl1.id = tbl2.id) 
WHEN MATCHED THEN 
  UPDATE SET tbl1.phone_number=123456;

update table1 set phone_number = 555555 where id not in (select id from table2);

Is there any way to achieve it by using only MERGE Statement ?

Just use an UPDATE statement:

Oracle Setup :

CREATE TABLE table1 ( id, phone_number ) AS
SELECT 1, 1 FROM DUAL UNION ALL
SELECT 2, 2 FROM DUAL;

CREATE TABLE table2 ( id ) AS
SELECT 1 FROM DUAL UNION ALL
SELECT 3 FROM DUAL;

Update :

UPDATE table1 t1
SET    phone_number = COALESCE(
                        (
                          SELECT 123456
                          FROM   table2 t2
                          WHERE  t1.id = t2.id
                        ),
                        555555
                      )

Output :

SELECT *
FROM   table1
\nID | PHONE_NUMBER\n-: |  -----------: \n 1 |  123456 \n 2 |  555555 \n

db<>fiddle here


The syntax you are looking for in a MERGE statement exists in SQL Server but is NOT valid in Oracle:

MERGE INTO table1 t1
USING table2 t2
ON ( t1.id = t2.id )
WHEN MATCHED THEN
  UPDATE SET phone_number = 123456
WHEN NOT MATCHED BY SOURCE THEN
  UPDATE SET phone_number = 555555;

db<>fiddle here

When no rows are matched then you can not use UPDATE (in WHEN NOT MATCHED ). as there are no rows matched then which data should be updated?

Normal merge statement must have following structure:

 MERGE <hint> INTO <table_name> USING <table_view_or_query> ON (<condition>) WHEN MATCHED THEN <update_clause> DELETE <where_clause> WHEN NOT MATCHED THEN <insert_clause> [LOG ERRORS <log_errors_clause> <reject limit <integer | unlimited>];

When there is no match then there are no rows found by oracle into your target table which matches with source table using ON condition then how can it update the record? which record it will update?

WHEN NOT MATCHED is illustrated as following in oracle documentation :

在此处输入图片说明

You can handle your scenario using MERGE as follows:

-- Oracle data creation

SQL> CREATE TABLE table1 ( id number, phone_number number ); Table created. SQL> INSERT INTO table1 2 SELECT 1, 111 from dual UNION ALL 3 SELECT 2, 222 from dual UNION ALL 4 SELECT 3, 333 from dual UNION ALL 5 SELECT 4, 444 from dual; 4 rows created. SQL> drop table table2; Table dropped. SQL> CREATE TABLE table2 ( id number ); Table created. SQL> INSERT INTO table2 2 SELECT 1 from dual UNION ALL 3 SELECT 2 from dual; 2 rows created.

-- Your merge statement

SQL> MERGE INTO TABLE1 TBL1 2 USING ( 3 SELECT T1.ID, T2.ID AS T2ID 4 FROM TABLE1 T1 5 LEFT JOIN TABLE2 T2 ON T1.ID = T2.ID 6 ) 7 TBL2 ON ( TBL1.ID = TBL2.ID ) 8 WHEN MATCHED THEN 9 UPDATE SET TBL1.PHONE_NUMBER = NVL2(TBL2.T2ID, 123456, 555555); 4 rows merged.

-- Result

SQL> SELECT * FROM TABLE1; ID PHONE_NUMBER ---------- ------------ 1 123456 2 123456 3 555555 4 555555 SQL>

Cheers!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM