有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语句??

解决方案 »

  1.   

    如果是同一台机器: 连A数据库
    select tab1.* from tab1 where tab1.id not in (select id from B..tab2))如果不是同一台机器,用opendatasource
      

  2.   

    这个SqlConnection怎么写啊?
      

  3.   

    如:cat
      select tab1.* from tab1 where tab1.id not in (select id from B..tab2))如果不是同一台机器,用opendatasource
     的话,
     sqlConnection 中的DBname='A',应该就可以了
      

  4.   

    SqlConnection con=new SqlConnection("S..database=A..");
      

  5.   

    select A.tab1 .* from A.tab1 where A.tab1.id not in (select id from B.tab2)
    --此处A/B表示两个数据库的名称,如果在一个数据库中调用 另一个数据库中的表时,可以这样来写:
    数据库名.表名
      

  6.   

    也可以这样来写:
    Select tab1.* From tab1.*
      Left Join B.tab2 ON tab1.ID<>B.tab2.ID