Need to check if your tables in 2 different servers are out-of-sync? SQL Server comes with a command line tool that does the job. It’s called tablediff.exe (read up BOL entry for this nifty tool).

By default, you can find this in the COM directory of your SQL Server install folder.

In SQL Server 2005 by default it will be in:
C:Program FilesMicrosoft SQL Server90

In SQL Server 2008 / SQL Server 2008 R2 by default it will be in:
C:Program FilesMicrosoft SQL Server100

These are the switches you can specify for the tool:

Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation.  All rights reserved.
C:UsersAdministrator>cd C:Program FilesMicrosoft SQL Server100COM
C:Program FilesMicrosoft SQL Server100COM>tablediff.exe /?
Microsoft (R) SQL Server Replication Diff Tool
Copyright (c) 2008 Microsoft Corporation
User-specified agent parameter values:
/?
 Replication Diff Tool Command Line Options
        usage: tablediff
          -- Source Options --
 -sourceserver          Source Host
 -sourcedatabase        Source Database
 -sourceschema          Source Schema Name
 -sourcetable           Source Table or View
 -sourceuser            Source Login
 -sourcepassword        Source Password
 -sourcelocked          Lock the source table/view durring tablediff
          -- Destination Options --
 -destinationserver     Destination Host
 -destinationdatabase   Destination Database
 -destinationschema     Destination Schema Name
 -destinationtable      Destination Table or View
 -destinationuser       Destination Login
 -destinationpassword   Destination Password
 -destinationlocked     Lock the destination table/view durring tablediff
          -- Misc Options --
 -t             Timeout
 -c             Column Level Diff
 -f             Generate Fix SQL (You may also specify a file name and path)
 -q             Quick Row Count
 -et            Specify a table to put the errors into
 -dt            Drop the error table if it exists
 -o             Output file
 -b             Number of bytes to read for blob data types
 -strict        Strict compare of source and destination schema
 -rc            Number of retries
 -ri            Retry interval

Here is an example usage. Note this has to be all in one line at the Command Prompt (I just separated them out into different lines for clarity):
 

C:Program FilesMicrosoft SQL Server100COM>tablediff.exe -sourceserver POSEIDON 
-sourcedatabase AdventureWorks2008R2 
-sourcetable Source_Department 
-destinationserver ZEUS 
-destinationdatabase AdventureWorks2008R2 
-destinationtable Destination_Department 
-f C:Tempsynctables_script.sql

The option -f allows me to specify a file name that will contain the SQL statements I need to run on the destination to “fix” and “synchronize” my tables.
Here is an example output:

Microsoft (R) SQL Server Replication Diff Tool
Copyright (c) 2008 Microsoft Corporation
User-specified agent parameter values:
-sourceserver POSEIDON
-sourcedatabase AdventureWorks2008R2
-sourcetable Source_Department
-destinationserver POSEIDON
-destinationdatabase AdventureWorks2008R2
-destinationtable Destination_Department
-f C:Tempsynctables_script.txt
Table [AdventureWorks2008R2].[dbo].[Source_Department] on POSEIDON and Table [AdventureWorks2008R2].
[dbo].[Destination_Department] on POSEIDON have 2 differences.
Fix SQL written to C:Tempsynctables_script.sql.
Err     DepartmentID    Col
Mismatch        5       Name
Src. Only       18
The requested operation took 0.4160742 seconds.

This is the resulting “fix” file:

-- Host: POSEIDON
-- Database: [AdventureWorks2008R2]
-- Table: [dbo].[Destination_Department]
SET IDENTITY_INSERT [dbo].[Destination_Department] ON
UPDATE [dbo].[Destination_Department] 
SET [Name]=N'Purchasing and Logistics' 
WHERE [DepartmentID] = 5
INSERT INTO [dbo].[Destination_Department] 
([DepartmentID],[GroupName],[ModifiedDate],[Name]) 
VALUES (18,N'New Department Group',N'2010-08-01 00:00:00.000',N'New Department')
SET IDENTITY_INSERT [dbo].[Destination_Department] OFF
VN:F [1.9.22_1171]
Rating: 9.0/10 (7 votes cast)
VN:F [1.9.22_1171]
Rating: +2 (from 2 votes)
tablediff.exe – Compare and Synchronize your SQL Server tables, 9.0 out of 10 based on 7 ratings  
Be Sociable, Share!
  • Tweet