MERGE INTO [your table-name] [rename your table here] USING ( [write your query here] )[rename your query-sql and using just like a table] ON ([conditional expression here] AND [...]...) WHEN MATCHED THEN [here you can execute some update sql or something else ] WHEN NOT MATCHED THEN [execute something else here ! ]
作用:判断B表和A表是否满足ON中条件,如果满足则执行满足后面的update SQL,如果不满足,则执行不满足后面的insert SQL。
例子:
使用情况,例如:如果数据库中存在数据就update,如果不存在就insert。
MERGE INTO TEST T1 USING (SELECT '2' as ID, 'newtest2' as NAME FROM dual) T2 on (T1.ID=T2.ID) WHEN MATCHED THEN UPDATE SET T1.NAME=T2.NAME WHEN NOT MATCHED THEN INSERT (T1.ID, T1.NAME) VALUES (T2.ID, T2.NAME );