日期:2014-05-18  浏览次数:20469 次

求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 B
delete 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.关联字段)