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。sql
例子:數據庫
使用狀況,例如:若是數據庫中存在數據就update,若是不存在就insert。express
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 );