The 'tablediff' utility is a new feature in SQL Server 2005 used to compare differences of schema and data between source and destination table or view. This utility is particularly useful while replicating data.

You can run this utility from the command line or a batch file.

Example:

Let us use the Customers table of the Northwind database as an example. Do the following :

1. Create another table called CustomerTemp.

SELECT * INTO CustomersTemp FROM Customers

2. Now change data in the CustomersTemp table


UPDATE CustomersTemp
SET City = 'Bern'
WHERE CUSTOMERID = 'ALFKI'

3. Run the TableDiff utility

The syntax for running this utility is as follows :

C:"Program Files"Microsoft SQL Server"90"COM>tablediff -sourceserver "SUPROTIM"-sourcedatabase "Northwind" -sourcetable "Customers" -destinationserver "SUPROTIM" -destinationdatabase "Northwind" -destinationtable "CustomersTemp"

where SUPROTIM is the servername, Northwind is the database, Customers is the source table and CustomerTemp is the destination table

The output is:

Microsoft (R) SQL Server Replication Diff ToolCopyright (C) 1988-2005 Microsoft Corporation. All rights reserved.
User-specified agent parameter values:-sourceserver SUPROTIM-sourcedatabase Northwind-sourcetable Customers-destinationserver SUPROTIM-destinationdatabase Northwind-destinationtable CustomersTemp
Table [Northwind].[dbo].[Customers] on SUPROTIM and Table [Northwind].[dbo].[CustomersTemp] on SUPROTIM have 1 differences.Err CustomerIDMismatch 'ALFKI'The requested operation took 0.244125 seconds.

If you want to generate the SQL scripts for the differences found, use the -f parameter with the file name:

Example :
C:"Program Files"Microsoft SQL Server"90"COM>tablediff -sourceserver "SUPROTIM"-sourcedatabase "Northwind" -sourcetable "Customers" -destinationserver "SUPROTIM" -destinationdatabase "Northwind" -destinationtable "CustomersTemp" -f "C:"temp"

posted on 2008-04-02 14:09  josephshi  阅读(312)  评论(0编辑  收藏  举报