当前位置: 代码迷 >> Sql Server >> 怎么判断一个表内的数据是否在另一个表内存在
  详细解决方案

怎么判断一个表内的数据是否在另一个表内存在

热度:51   发布时间:2016-04-27 17:49:10.0
如何判断一个表内的数据是否在另一个表内存在
我现在有一个数据表A和数据表B,现在我想判断数据表B内的数据在数据表A内是否已经存在,将不存在的数据从B导插入到A中,请问这个该怎么操作啊?
可以用VS写也可以用Tsql写

------解决方案--------------------
insert into A
select * from B
where not exists(select 1 from A where A.主键=B.主键)
------解决方案--------------------
insert into a
select * from b where id not in (select id from a)
------解决方案--------------------
insert into b select * from a where id not in (select id from b)
  相关解决方案