if not exists (select * from A where (select count(1) from B where A.iID = B.IID) <> 0
)
insert into a (iid,objid,sname,SCODE)
select  bb.iid,bb.objid,bb.sname,bb.scode from B bb

else Update a Set
objid=dbo.B.objid,sname=dbo.B.sname,scode =dbo.B.scode  from dbo.B where  dbo.A.IID=dbo.b.iid

 

使用场景:

 两个数据表,暂定A表和B表,如A表在B表数据,则更新A表数据,如A表不存在B表数据,则插入数据,使用前提,两个表有唯一主健,如ID

使用此方法存在不足,B表删除,A表数据不会删除,所以在程序控制中,一般做逻辑删除DFG删除标记。