7

I have recently discovered the tablediff utility of SQL Server 2005.

I have 2 instances of the same database each on a different server.

Is it possible to compare all tables using tablediff without having to replicate the same command while only changing the table name?

For example, compare table1 on server1 with table1 on server2 then compare table2 on server1 with table2 on server2, until all tables have been compared.

3 Answers 3

10

This can be done by setting the -sourceserver option of the tablediff utility to the first server and the -destinationserver option to the second server. You can use the sys.Tables to iterate through each table in the database so that you can automate this process.

Edited

I also wanted to point out this article which is a clever piece of t-sql code that may serve you better without the complications of tablediff

Per your comment, here is an example. This is not an optimal way to do this in a production enviorment, but it should get the job done for you. You would probably be better off implementing this in SSIS if you need a more production worthy option.

SET QUOTED_IDENTIFIER ON 

DECLARE @TableNames as table (
    id int identity(1,1),
    tableName varchar(100))

DECLARE @sTableDiff nvarchar(1000)
DECLARE @tableName varchar(100)
DECLARE @counter int
DECLARE @maxCount int

INSERT INTo @TableNames 
SELECT name 
FROM sysobjects WHERE type = 'U'

SET @counter = 1

SELECT @maxCount = COUNT(name) 
FROM sysobjects WHERE type = 'U'

WHILE @counter < @maxCount
    Begin
        SELECT @tableName = tableName 
        FROM @TableNames 
        WHERE id = @counter

        SET @sTableDiff= ' "C:\Program Files\Microsoft SQL Server\90\COM\tablediff" -sourceserver Server1 
            -sourceuser sa -sourcepassword password -sourcedatabase YourDatabase -sourcetable ' + @tableName + ' 
            -destinationserver Server2 -destinationuser sa -destinationpassword password -destinationdatabase 
            YourDatabase -destinationtable ' + @tableName + '  -f c:\Diff'      

        EXEC XP_CMDSHELL @sTableDiff

        Set @counter = @counter + 1
    End
4
  • Can u provide an example? Thanks!
    – Davie
    Nov 12, 2009 at 15:46
  • When I run this sql script there is an error where tablediff says it cant connect to the database. However, when I run the exact same query in a dos prompt it works without any problems i.e tablediff reports that the 2 tables are identical! Any ideas?
    – Davie
    Nov 13, 2009 at 13:20
  • I am not sure, this is a script that I have used many times, so I know that it works. If you add Print @sTableDiff to the script and then execute the output in the command prompt, does that work? Nov 13, 2009 at 13:59
  • +1 and a "bump!" for the linked article at weblogs.sqlteam.com/jeffs/archive/2004/11/10/2737.aspx and the elegant comparison query shown therein. Sep 27, 2010 at 8:27
1

Try dbForge Scheme Compare for SQL server and dbForge Data Compare for SQL Server. It can compare and synchronize any database data and scheme. Quick, easy, always delivering a correct result.

Run it up on you database!

0

Can you make use this option to run the sql statements in a script file and compare? I may be wrong just a thought.

-bf number_of_statements

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.