日期:2014-05-19  浏览次数:20379 次

2不同数据库的比较(50分求解)
有2个数据库A,B,我要将A的表tab1跟B的表tab2进行比较

tab1   和   tab2有个共同的字段ID,2个表中的ID有些是相同的,我就是要把不同的取出

如果tab1和tab2在同一个数据库中,用个not   in这样就可以了
(select   tab1.*   from   tab1   where   tab1.id   not   in   (select   id   from   tab2))

但是现在tab1和tab2在A,B两个不同的数据库中,这样的情况怎么写sql语句??



------解决方案--------------------
如果是同一台机器: 连A数据库
select tab1.* from tab1 where tab1.id not in (select id from B..tab2))

如果不是同一台机器,用opendatasource

------解决方案--------------------
如:cat
select tab1.* from tab1 where tab1.id not in (select id from B..tab2))

如果不是同一台机器,用opendatasource
的话,
sqlConnection 中的DBname= 'A ',应该就可以了
------解决方案--------------------
select A.tab1 .* from A.tab1 where A.tab1.id not in (select id from B.tab2)
--此处A/B表示两个数据库的名称,如果在一个数据库中调用 另一个数据库中的表时,可以这样来写:
数据库名.表名