create table testsou( --源测试表
id int,
name varchar(10),
des varchar(50)
)
create table testtag( --目标表
id int,
name varchar(10),
des varchar(50)
)
insert into testsou values(1,'small','a small dog'),(2,'big','a big cat'),(3,'middle','middle school');
insert into testtag values(1,'small','a small dog'),(2,'big','HEHE'),(4,'HEHE','');
merge into testtag t
using testsou s
on t.id=s.id and t.name=s.name
when matched --and (t.des<>s.des)/*去掉则无论是否相等都更新,多个字段更新用OR连接*/
then--ON条件成立且需更新时update
update set t.des=s.des
when not matched then--不成立时插入
insert (id,name,des)/*(字段信息)*/ values(s.id,s.name,s.des)
--;下面可选
when not matched by source then
delete;
select * from testsou;
select * from testtag;
drop table testsou;
drop table testtag;
转载请注明原文地址: https://www.6miu.com/read-15138.html