如果你的数据库支持minus的话,那很容易了
select * from table1
minus
select * from table2
就能得到table1中存在而table2中不存在的纪录了,反之就能得到table2中存在而table1中不存在的纪录

解决方案 »

  1.   

    我感觉可以做一个存储过程,定义一个指向TABLE1的游标,建一个临时表1,这个临时表比你这两个表多两条字段,一个表示这条记录属于哪个表,另外一个表示有什么不同,如:在TABLE2中不存在或者存在但各个字段的值不同,或者其他,然后遍历TABLE1,在TABLE2中查找TABLE1的游标所指向的记录,找到的话进行比较。
    然后再遍历TABLE2。
      

  2.   

    select * from table1
    where exists ( select * from table2
    where table1.pkey = table2.pkey
    and( table1.field1 <> table2.field1
    or table1.field2 <> table2.field2
    ...
    or table1.fieldn <> table2.fieldn));
      

  3.   

    select *  ----在table1但不在table2中
      from table1 as t1
      where not exists(
        select * 
          from table2 as t2
          where t1.f1=t2.f1 and t1.f2=t2.f2 and ....
        )
    union 
    select *  ----在table2但不在table1中
      from table2 as t1
      where not exists(
        select * 
          from table1 as t2
          where t1.f1=t2.f1 and t1.f2=t2.f2 and ....
        )
      

  4.   

    一个相同的问题,呵呵:(oracle)
    http://www.itpub.net/showthread.php?s=&threadid=21181
      

  5.   

    如果支持minus的话,就用弱水兄的方法.
      

  6.   

    我用的是SQL SERVER好像没见过minus
    还有我的两个表中对应的字段的字段名不同这个minus可能不好用吧?
    我的想法是在程序中比较
    取出两个结果集,按主键排序,然后从两个结果集的第一条纪录开始比较
    主键,若主键相同则再比较其他字段的内容,若主键不同,则根据两个主键
    的大小来判断两个表中两条纪录的关系。这个想法应该可以实现的,就是
    不知道效率如何?望大家指教!
    楼上的sql语句好像可以呀,不知道效率如何,试试先!
      

  7.   

    试试except,在db2中是用except:select * from table1 except select * from table2.
      

  8.   

    sql server中有没有except呀?
    顶上去!