Compare data between two tables

本文介绍了 SQL Server 2005 中的新特性 'TableDiff' 实用程序,该工具用于比较源表和目标表之间的模式和数据差异。通过一个具体的示例演示了如何使用此工具来检测 Northwind 数据库中 Customer 表与其副本表 CustomersTemp 之间的差异,并生成 SQL 脚本来更新这些差异。


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"

转载于:https://www.cnblogs.com/josephshi/archive/2008/04/02/1134525.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值