有两个数据库,所有表结构都一样,现在需要比较数据库A和数据库B。
把数据库B中的所有表的记录不再数据库A对应表的记录插入到数据库A对应表中
比如数据库A和B都有table表
数据库A TABLE表记录:
FIELD1 FIELD2 FIELD3
1 2 3
数据库B TABLE 表记录
FIELD1 FIELD2 FIELD3
1 2 3
4 5 6
数据库B表的 4,5,6这条记录不在数据库A 的table表中就把这条记录插入到数据库A 的表中该怎么比对呢?
把数据库B中的所有表的记录不再数据库A对应表的记录插入到数据库A对应表中
比如数据库A和B都有table表
数据库A TABLE表记录:
FIELD1 FIELD2 FIELD3
1 2 3
数据库B TABLE 表记录
FIELD1 FIELD2 FIELD3
1 2 3
4 5 6
数据库B表的 4,5,6这条记录不在数据库A 的table表中就把这条记录插入到数据库A 的表中该怎么比对呢?
然后 在数据库A 执行:insert into table_name (FIELD1, FIELD2 ,FIELD3)
select FIELD1, FIELD2 ,FIELD3 from db_links.[数据库A]..table_name t
where not exists(select 1 from table_name where FIELD1=t.FIELD1 and FIELD2=t.FIELD2
and FIELD3=t.FIELD3)
insert into A.dbo.table1
select * from B.dbo.table1 where not exists(select 1 from A.dbo.table1 where field1=B.dbo.table1.field1)