How to compare data between two table in different databases using Sql Server 2008?

前端 未结 9 2248
一生所求
一生所求 2020-12-13 17:41

I have two database\'s, named DB1 and DB2 in Sql server 2008. These two database\'s have the same tables and same table data also. However, I want to check if there are an

相关标签:
9条回答
  • 2020-12-13 17:52

    Comparing the two Databases in SQL Database. Try this Query it may help.

    SELECT T.[name] AS [table_name], AC.[name] AS [column_name],  TY.[name] AS 
       system_data_type FROM    [***Database Name 1***].sys.[tables] AS T  
       INNER JOIN [***Database Name 1***].sys.[all_columns] AC ON T.[object_id] = AC.[object_id]      
       INNER JOIN [***Database Name 1***].sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id] 
       EXCEPT SELECT T.[name] AS [table_name], AC.[name] AS [column_name], TY.[name] AS system_data_type FROM    ***Database Name 2***.sys.[tables] AS T  
       INNER JOIN ***Database Name 2***.sys.[all_columns] AC ON T.[object_id] = AC.[object_id]  
       INNER JOIN ***Database Name 2***.sys.[types] TY ON AC.[system_type_id] = TY.[system_type_id]
    
    0 讨论(0)
  • 2020-12-13 17:53

    If the database are in the same server use [DatabaseName].[Owner].[TableName] format when accessing a table that resides in a different database.

    Eg: [DB1].[dbo].[TableName]

    If databases in different server look at on Creating Linked Servers (SQL Server Database Engine)

    0 讨论(0)
  • 2020-12-13 17:53

    Another solution (non T-SQL): you can use tablediff utility. For example if you want to compare two tables (Localitate) from two different servers (ROBUH01 & ROBUH02) you can use this shell command:

    C:\Program Files\Microsoft SQL Server\100\COM>tablediff -sourceserver ROBUH01 -s
    ourcedatabase SIM01 -sourceschema dbo -sourcetable Localitate -destinationserver
     ROBUH02 -destinationschema dbo -destinationdatabase SIM02 -destinationtable Lo
    calitate
    

    Results:

    Microsoft (R) SQL Server Replication Diff Tool Copyright (c) 2008 Microsoft Corporation User-specified agent parameter values: 
    -sourceserver ROBUH01 
    -sourcedatabase SIM01 
    -sourceschema dbo 
    -sourcetable Localitate 
    -destinationserver ROBUH02 
    -destinationschema dbo 
    -destinationdatabase SIM02 
    -destinationtable Localitate 
    
    Table [SIM01].[dbo].[Localitate] on ROBUH01 and Table [SIM02].[dbo].[Localitate ] on ROBUH02 have 10 differences. 
    
    Err Id Dest. 
    Only 21433 Dest. 
    Only 21434 Dest. 
    Only 21435 Dest. 
    Only 21436 Dest. 
    Only 21437 Dest. 
    Only 21438 Dest. 
    Only 21439 Dest. 
    Only 21441 Dest. 
    Only 21442 Dest. 
    Only 21443 
    The requested operation took 9,9472657 seconds.
    ------------------------------------------------------------------------
    
    0 讨论(0)
  • 2020-12-13 17:55
    select * 
    from (
          select 'T1' T, *
          from DB1.dbo.Table
          except
          select 'T2' T, *
          from DB2.dbo.Table
         ) as T
    union all
    select * 
    from (
          select 'T2' T, *
          from DB2.dbo.Table
          except
          select 'T1' T, *
          from DB1.dbo.Table
         ) as T
    ORDER BY 2,3,4, ..., 1  -- make T1 and T2 to be close in output 2,3,4 are UNIQUE KEY SEGMENTS
    

    Test code:

    declare @T1 table (ID int)
    declare @T2 table (ID int)
    
    insert into @T1 values(1),(2)
    insert into @T2 values(2),(3)
    
    select * 
    from (
          select *
          from @T1
          except
          select *
          from @T2
         ) as T
    union all
    select * 
    from (
          select *
          from @T2
          except
          select *
          from @T1
         ) as T
    

    Result:

    ID
    -----------
    1
    3
    

    Note: It can take long time to compare big table, when developing "tuned" solution or refactorig, which will give same result as REFERERCE - it may be wise to chekc simple parameters first: like

    select count(t.*) from (
       select count(*) c0, SUM(BINARY_CHECKSUM(*)%1000000) c1 FROM T_REF_TABLE 
       -- select 12345 c0, -214365454 c1 -- constant values FROM T_REF_TABLE 
       except 
       select count(*) , SUM(BINARY_CHECKSUM(*)%1000000) FROM T_WORK_COPY 
    ) t
    

    When this is empty, you have probably things under controll, and may be you can modify when you fail you will see "constant values FROM T_REF" to isert to save even more time for next check!!!

    0 讨论(0)
  • 2020-12-13 17:56

    I’d really suggest that people who encounter this problem go and find a third party database comparison tool.

    Reason – these tools save a lot of time and make the process less error prone.

    I’ve used comparison tools from ApexSQL (Diff and Data Diff) but you can’t go wrong with other tools marc_s and Marina Nastenko already pointed out.

    If you’re absolutely sure that you are only going to compare tables once then SQL is fine but if you’re going to need this from time to time you’ll be better off with some 3rd party tool.

    If you don’t have budget to buy it then just use it in trial mode to get the job done.

    I hope new readers will find this useful even though it’s a late answer…

    0 讨论(0)
  • 2020-12-13 17:57

    I'v done things like this using the Checksum(*) function

    In essance it creates a row level checksum on all the columns data, you could then compare the checksum of each row for each table to each other, use a left join, to find rows that are different.

    Hope that made sense...

    Better with an example....

    select *
    from 
    ( select checksum(*) as chk, userid as k from UserAccounts) as t1
    left join 
    ( select checksum(*) as chk, userid as k from UserAccounts) as t2 on t1.k = t2.k
    where t1.chk <> t2.chk 
    
    0 讨论(0)
提交回复
热议问题