SQL Server 2005 TableDiff Utility | Database Journal

SQL Server 2005 TableDiff Utility

Mar 29, 2006
3 minute read

One of the many command line utilities that came with SQL Server 2005 is the TableDiff utility. Although this utility is primarily designed for comparing replicated tables, we can take advantage of its functionality to compare tables. The objective of this article is to demonstrate the TableDiff utility.


Let us assume that we have the following databases and table.


Step 1

set quoted_identifier off
go
USE [master]
GO
/****** Object:  Database [MyDB1]    Script Date: 03/22/2006 08:08:48 ******/
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N’MyDB1′)
DROP DATABASE [MyDB1]
go
create database MyDB1
go
use MyDB1
go
create table ABC (id int primary key, name varchar(100),salary money)
go
insert into ABC select 1,”Catherine Donnel”,200000
insert into ABC select 2,”Stacey Kost”,150000
insert into ABC select 3,”Jason Leanos”,36000
insert into ABC select 4,”Catherine O’Donnel”,20000
go
USE [master]
GO
/****** Object:  Database [MyDB2]    Script Date: 03/22/2006 08:09:12 ******/
IF  EXISTS (SELECT name FROM sys.databases WHERE name = N’MyDB2′)
DROP DATABASE [MyDB2]
go
Create database MyDB2
go
use MyDB2
go
create table ABC (id int primary key, name varchar(100),salary money)
go
insert into ABC select 1,”Catherine O’Donnel”,220000
insert into ABC select 2,”Stacey Kostue”,230000
insert into ABC select 4,”Catherine O’Donnel”,20000
insert into ABC select 9,”Irina Zolotrova”,40000
go


Step 2


Go to the command prompt [Click Start – Run – Cmd.exe – press enter] and type the following commands. [refer Fig 1.0]


Note: Change the path depending on your installation configuration. The TableDiff utility is installed under the


Program Files\Microsoft SQL Server\90\COM folder

C:\> cd “C:\Program Files\Microsoft SQL Server\90\COM”
C:\Program Files\Microsoft SQL Server\90\COM>tablediff -sourceserver “SQL2005” –
sourcedatabase “MyDB1” -sourcetable “ABC” -destinationserver “SQL2005” -destinat
iondatabase “MyDB2” -destinationtable “ABC”



Result

User-specified agent parameter values:
-c
-sourceserver SQL2005
-sourcedatabase MyDB1
-sourcetable ABC
-destinationserver SQL2005
-destinationdatabase MyDB2
-destinationtable ABC
Table [MyDB1].[dbo].[ABC] on SQL2005 and Table [MyDB2].[dbo].[ABC]
 on SQL2005 have 4 differences.
Err     id      Col
Mismatch        1       name salary
Mismatch        2       name salary
Src. Only       3
Dest. Only      9
The requested operation took 0.3905616 seconds.
Advertisement


Step 3


Another cool function of the TableDiff utility is the generation of SQL Scripts for the differences that it found. Let us re-run the TableDiff utility by adding another –f parameter.

C:\Program Files\Microsoft SQL Server\90\COM>tablediff -sourceserver “SQL2005” –
sourcedatabase “MyDB1” -sourcetable “ABC” -destinationserver “SQL2005” -destinat
iondatabase “MyDB2” -destinationtable “ABC”  -f “c:\Diff”


Result

-sourceserver SQL2005
-sourcedatabase MyDB1
-sourcetable ABC
-destinationserver SQL2005
-destinationdatabase MyDB2
-destinationtable ABC
-f c:\Diff
Table [MyDB1].[dbo].[ABC] on SQL2005 and Table [MyDB2].[dbo].[ABC] on SQL2005 ha
ve 4 differences.
Fix SQL written to c:\Diff.sql.
Err     id      Col
Mismatch        1       name salary
Mismatch        2       name salary
Src. Only       3
Dest. Only      9
The requested operation took 0.3905616 seconds.


This time the TableDiff Utility also created C:\Diff.SQL file as shown below. [refer Fig 1.1]

— Host: SQL2005
— Database: [MyDB2]
— Table: [dbo].[ABC]
UPDATE [dbo].[ABC] SET [name]=’Catherine Donnel’,[salary]=200000.0000 WHERE [id] = 1
UPDATE [dbo].[ABC] SET [name]=’Stacey Kost’,[salary]=150000.0000 WHERE [id] = 2
INSERT INTO [dbo].[ABC] ([id],[name],[salary]) VALUES (3,’Jason Leanos’,36000.0000)
DELETE FROM [dbo].[ABC] WHERE [id] = 9



Fig 1.1


Other options can be used in the TableDiff Utility. They can be found by executing the following command:

C:\Program Files\Microsoft SQL Server\90\COM>tablediff /?


























— Source Options —
-sourceserverSource Host
-sourcedatabaseSource Database
-sourceschemaSource Schema Name
-sourcetableSource Table or View
-sourceuserSource Login
-sourcepasswordSource Password
-sourcelockedLock the source table/view durring tablediff


 


























— Destination Options —
Note: For multiple Destinations use a comma delimited list
-destinationserverDestination Host
-destinationdatabaseDestination Database
-destinationschemaDestination Schema Name
-destinationtableDestination Table or View
-destinationuserDestination Login
-destinationpasswordDestination Password
-destinationlockedLock the destination table/view durring tablediff


 





























— Misc Options –
-cColumn Level Diff
-fGenerate Fix SQL (You may also specify a file name and path)
-qQuick Row Count
-etSpecify a table to put the errors into
-dtDrop the error table if it exists
-oOutput file
-bNumber of bytes to read for blob data types (Default 1000)
-strictStrict compare of source and destination schema
Advertisement


Conclusion


This article has demonstrated the major function of the TableDiff utility.


»See All Articles by ColumnistMAK

Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.