当前位置: 代码迷 >> Sql Server >> 求2表数据比较sql,该如何处理
  详细解决方案

求2表数据比较sql,该如何处理

热度:74   发布时间:2016-04-27 15:04:36.0
求2表数据比较sql
sqlserver2000

2章表的数据结构相同

现在需要将A表数据跟B表数据做比对

当发现A中某一字段的值在B中已经出现,则将A中该条记录整体更新到B中;当发现B中没有该记录时,则将A中该条记录插入B中

------解决方案--------------------
SQL code
delete A from B,A where A.某一字段=B.某一字段insert into B select * from A  where not exists(select 1 from B where 某一字段=A.某一字段)
------解决方案--------------------
SQL code
--delete Bdelete B from B,A where A.某一字段=B.某一字段insert into B select * from A  where not exists(select 1 from B where 某一字段=A.某一字段)
------解决方案--------------------
+1
探讨
SQL code

--delete B
delete B from B,A where A.某一字段=B.某一字段
insert into B
select * from A
where not exists(select 1 from B where 某一字段=A.某一字段)

------解决方案--------------------
SQL code
update b set col1=a.col1 .... from a join b on a.关联字段=b.关联字段 insert into b select * from a where not exists(select 1 from b where 关联字段=a.关联字段)
  相关解决方案