所谓的数据一致就是库表里的数据数据相同。比如我对原有的数据库进行了修改,但发现有些数据不全,所以我就把我的备份恢复成另外一个库,对新旧库进行比较。我知道可以用CONVERT把所有的字段值相加进行比较,但由于数据量太大,所以比较的时候会死或耗时太长,有更好的办法吗?

解决方案 »

  1.   

    如果你的表有主键,可以这样比较(假设主键为ID):1、A库有B库没有:
    select * from a..tablename a
    where not exists (
    select 1 from b..tablename b
    where a.id=b.id
    )2、B库有A库没有:
    select * from b..tablename b
    where not exists (
    select 1 from a..tablename a
    where a.id=b.id
    )3、B库A库不同:
    select * from a..tablename a,b..tablename b
    where a.id=b.id
    and a.col1<>b.col1
    union all
    select * from a..tablename a,b..tablename b
    where a.id=b.id
    and a.col2<>b.col2...union all
    select * from a..tablename a,b..tablename b
    where a.id=b.id
    and a.colN<>b.colN也可以不必用union all,因为修正数据才是你的目的,可以分别查询分别修改。