读书人

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

发布时间: 2012-03-13 11:21:11 作者: rapoo

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

读书人网 >SQL Server

热点推荐