简体   繁体   English

使用多个 UPDATE 语句合并

[英]MERGE with multiple UPDATE statements

Can we achieve below scenario using single MERGE statement:我们可以使用单个 MERGE 语句实现以下场景:

source table - table1 destination table- table2源表 - 表 1 目标表 - 表 2

when table1.id in table2.id then update table1 SET phone_number=123456当 table1.id 在 table2.id 然后更新 table1 SET phone_number=123456

when table1.id not in table2.id then update table1 SET phone_number=555555当 table1.id 不在 table2.id 然后更新 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 ?有没有办法只使用 MERGE Statement 来实现它?

Just use an UPDATE statement:只需使用UPDATE语句:

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 | 1 | 123456 123456\n 2 | 2 | 555555 555555\n

db<>fiddle here db<> 在这里小提琴


The syntax you are looking for in a MERGE statement exists in SQL Server but is NOT valid in Oracle:你正在寻找一个语法MERGE在SQL Server中存在语句,但不是有效的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 db<> 在这里摆弄

When no rows are matched then you can not use UPDATE (in WHEN NOT MATCHED ).如果没有匹配的行,则不能使用UPDATE (在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?当没有匹配时,oracle 在target表中找不到与使用ON条件匹配的source表的行,那么它如何更新记录? which record it will update?它将更新哪个记录?

WHEN NOT MATCHED is illustrated as following in oracle documentation :oracle 文档中, WHEN NOT MATCHED如下所示:

在此处输入图片说明

You can handle your scenario using MERGE as follows:您可以使用MERGE处理您的场景,如下所示:

-- Oracle data creation -- Oracle 数据创建

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!!干杯!!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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