这个是比较A表和B表的值,如果通过A表的ID字段和B表的ID字段做比较,如果A表不存在就插入,如果存在就修改。
merge into A_table a
using B_table b
on(a.id=b.id)
when matched then --如果A表中存在B表的ID,就修改他的名字
update set a.name=b.name
when not matched then --如果不存在,就将B表的ID和名字插入。
insert (a.id,a.name)
values (b.id,b.name);
commit;
see watch look的区别?
利用一个表的数据来更新另一个表的数据,用merge可以提高更新的效率,它其实也是DML语句。
语法如下:
merge into A_table a
using B_table b
on(a.id=b.id) --匹配的条件
when matched then --如果满足匹配条件就做update
update set a.name=b.name
when not matched then --如果不满足匹配条件就做insert
insert (a.id,a.name)
values (b.id,b.name);