【发布时间】:2020-04-12 01:53:58
【问题描述】:
我们能否使用单个 MERGE 语句实现以下场景:
源表 - table1 目标表-table2
当 table1.id 在 table2.id 然后更新table1 SET phone_number=123456
当 table1.id 不在 table2.id 中时 然后更新 table1 SET phone_number=555555
注意:- 我可以使用以下查询来获得结果。
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);
有没有办法只使用 MERGE 语句来实现它?
【问题讨论】:
-
不匹配的时候可以试试?
-
@ErsinGülbahar
WHEN NOT MATCHED适用于值在table2中但不在table1中的情况。 OP 似乎想要等效于 SQL Server 语法WHEN NOT MATCHED BY [SOURCE|TARGET](在 Oracle 中不存在),以便当table2中没有值时,它们可以更新table1中的值。