當對兩個表(某個字段有相同的值,也就是匹配的列)進行比較更新/插入時剖毯,可以使用merge算法,邏輯為:
- 使用test1與test2對比疯兼;
- 設置關(guān)聯(lián)的字段;
- 當關(guān)聯(lián)的字段匹配到時:
- 設置需要進行的操作(更新贫途?插入吧彪?還是pass?)丢早;
- 當關(guān)聯(lián)的字段匹配不上時:
- 設置需要進行的操作(更新姨裸?插入?還是pass怨酝?)
示例:
merge into test2
using test1
on (test1.eid = test2.eid)
when matched then
update set name = test1.name, birth = test1.birth, salary = test1.salary
when not matched then
insert (eid,name, birth, salary)
values
(test1.eid, test1.name,test1.birth, test1.salary);