整理帖子:http://www.tiantiansoft.com/bbs/dispbbs.asp?boardID=66&ID=156919
以前用游标的方法:user1/9/archives/2007/3570.html
这个问题实际上就是两步:
1、把 B 表中存在的,A 表中不存在的数据,添加进入 A 表
2、把 A 表中存在的,B 表中不存在的数据,从 A 表删除
3、把 B 表中的数据,对应更新到 A 表中
create table A ( id int )
create table B ( id int )
insert into A ( id ) values ( 1 )
insert into A ( id ) values ( 2 )
insert into A ( id ) values ( 3 )
insert into A ( id ) values ( 5 )
insert into B ( id ) values ( 1 )
insert into B ( id ) values ( 7 )
insert into B ( id ) values ( 5 )
select * from A
select * from B
insert into A ( id )
select id from B
where not exists (select 1 from A where A.id = B.id)
delete from A
where id not in (select A.id from A,B where A.id = B.id)
update A
set id = B.id
from B
where A.id = B.id
select * from A
select * from B