相同服务器之间的数据映射,但具有相同表名的不同数据库

时间:2012-05-03 10:42:42

标签: sql sql-server-2008

嗨,我被困在这里需要你的建议。我有一个有多个DB的服务器。 现在我想映射一个表中1 db的数据是否等于具有相同表名的另一个db中的数据

谁能建议怎么做?谢谢advancve

2 个答案:

答案 0 :(得分:1)

select * from db1.table1 t1
full outer join db2.table2 t2 on t1.id = t2.id
where t1.id <> t2.id
or t1.col2 <> t2.col2 or ...

答案 1 :(得分:0)

取决于您需要映射的内容。 如果您只是想通过主键了解差异,我会尝试在PK上进行完全连接,因此它会告诉您A上存在但不存在于B上的记录以及存在于B但不存在于A上的记录。如下所示:

create table DB_A(id int)
create table DB_B(id int)

insert into DB_A values (1)
insert into DB_A values (2)

insert into DB_B values (2)
insert into DB_B values (3)

select DB_A.ID as 'Exists on A but not on B', DB_B.id as  'Exists on B but not on A'
from DB_A full join DB_B on DB_A.id=DB_B.id 
where DB_A.id is null or DB_B.id is null  

如果您需要的不仅仅是比较所有列的值,我建议您使用数据比较工具。使用SQL

来做这件事并不是那么紧张