【sqlserver】merge into test

来源:互联网 发布:python数据挖掘常用包 编辑:程序博客网 时间:2024/06/05 00:04
--merge into test/*使用Merge关键字的好处:简洁有效局限:在SQL Server 2008之前没有Merge*/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 tusing testsou son t.id=s.id and t.name=s.namewhen matched --ON条件成立时更新then update set t.des=s.deswhen not matched --不成立时插入then insert /*(字段信息)*/ values(s.id,s.name,s.des)when not matched by source --目标表存在,源表不存在的情况(删除)then delete;select * from testsou;select * from testtag;--DATA SYNC testsou和testtag数据一致drop table testsou;drop table testtag;
0 0
原创粉丝点击