两个数据库表结构完全相同,要比较一下两数据库中相同表的所有数据(记录数,每条记录的值)是否相同。比如:有两个数据库A和B,结构完全相同。 比较一下A数据库下所有表中的数据是否和B数据库中所有表中的数据 完全相同。多谢!

解决方案 »

  1.   

    之前用的一个数据库比较工具不错:
    sqldelta
      

  2.   


    查看數據就有點麻煩了,遊標循環SYS.OBJECTS 中的類別='U'再循環表中的列進行動態生成以下結構的語句進行查詢
    select *from A JOIN B ON A.ID<>B.ID AND A.NAME<>B.NAME。。
    當然這樣很慢的
    期待高手出現 
      

  3.   

    古老的方法:先用 SELECT name from sysobjects WHERE xtype = 'u'查出数据库中所有用户表然后用 select name from syscolumns  where id=....查出所有字段 最后一一比较,不过这种做法的效率太低了.
      

  4.   

    用 redgate 的 sql data compare
      

  5.   

    use b
    go
    declare @sql varchar(max)
    set @sql=''
    select @sql=@sql+'if exists(select * from a..'+name+' where checksum(*) not in (select checksum(*) from '+name+')
    union all
    select * from '+name+' where checksum(*) not in (select checksum(*) from a..'+name+')) print '''+name+''''+char(13)
     from sysobjects where type='u'
    exec(@sql)
      

  6.   


    -- 1、先比较表名称和架构
    --1.1
    select name, schema_name(schema_id) [schema] from db1.sys.objects where type = 'U'
    except
    select name, schema_name(schema_id) [schema] from db2.sys.objects where type = 'U'
    --1.2
    select name, schema_name(schema_id) [schema] from db2.sys.objects where type = 'U'
    except
    select name, schema_name(schema_id) [schema] from db1.sys.objects where type = 'U'-- 2、比较记录数
    select a.name, schema_name(a.schema_id) [schema], b.rows from db1.sys.objects a with(nolock) inner join db1.sys.partitions b with(nolock) on a.object_id = b.object_id where a.type = 'U' and b.index_id in (0, 1)
    except
    select a.name, schema_name(a.schema_id) [schema], b.rows from db2.sys.objects a with(nolock) inner join db2.sys.partitions b with(nolock) on a.object_id = b.object_id where a.type = 'U' and b.index_id in (0, 1)-- 3、如果上面都同就只能每个表去except了。
    这是我想出来的笨办法,不知道2楼所说的 sqldelta 工具是怎么实现的。
      

  7.   


    --比较一个表,比如users表:
    if exists(select * from a..users where checksum(*) not in (select checksum(*) from users)  
    union all
    select * from users where checksum(*) not in (select checksum(*) from a..users)) 
    print 'users'--上面就根据对象表来生成一批这样的语句即可
      

  8.   

    把这两个数据库中的所有相同名称的用户表的表名都读到一个临时表,然后用游标去一条一条的判断表中的数据是否相同了这就要用到checksum(*)了select count(*) as cou from abs.dbo.atb where checksum(*) not in (select checksum(*) from bbs.dbo.atb)select count(*) as cou from bbs.dbo.atb where checksum(*) not in (select checksum(*) from abs.dbo.atb)要这两个查询语句查到的值都小于1才表示不存在不同的记录。
      

  9.   

    tablediff 不就可以了?
    微软自带的工具
    在TOOLS下。
      

  10.   

    有一个关于SQL server的工具。。
    我以前看到过的好像是在www.codeproject.com 里面有的。  
      

  11.   

    我用过toad for sqlserver,比较结构与数据
      

  12.   


    SELECT * FROM A WHERE NOT EXISTS(SELECT * FROM B)SELECT * FROM B WHERE NOT EXISTS(SELECT * FROM A)