Database/MS-SQL

[스크랩] SQL SERVER REPLICATION DIFFTOOL (tablediff.exe)

99iberty 2017. 6. 8. 11:11


http://ceusee.tistory.com/187


SQL SERVER 2005 이상

 

 

와 어찌 하다가 이제서야 알게됨.

TABLEDIFF 유틸..   이름에서도 알 수 있 듯이 테이블 차이점 찾기 유틸입니다.

 

복제되어 있는 곳에서 데이터 불일치 문제를 해결하는데 유용하며,

batch file일 이용해서 사용 할 수 있습니다.

 

  • 복제 게시자 역할을 하는 MicrosoftSQL Server 인스턴스에 있는 원본 테이블과 복제 구독자 역할을 하는 하나 이상의 SQL Server 인스턴스에 있는 대상 테이블을 행 단위로 비교할 수 있습니다.

  • 행 개수와 스키마만 비교하여 비교 작업을 빨리 수행할 수 있습니다.

  • 열 수준에서 비교할 수 있습니다.

  • 대상 서버의 불일치를 해결하는 Transact-SQL 스크립트를 생성하여 원본 테이블과 대상 테이블을 일치시킬 수 있습니다.

  • 결과를 출력 파일이나 대상 데이터베이스의 테이블에 기록할 수 있습니다.

 

 

 

 

데이터 형식이 sql_variant 인 열은 지원하지 않는다.

 

도움말 :http://msdn.microsoft.com/ko-kr/library/ms162843(v=SQL.100).aspx

 

  • 사용권한
 비교할 테이블 개체에 대한 select all 권한이 있어야 함.

-o 또는 -f 옵션을 사용하려면 지정된 파일 디렉터리 위치에 대한 쓰기 권한이 있어야 함.

-et 옵션을 사용하려면 db_owner 고정 데이터베이스 역할의 멤버이거나 적어도 구독 데이터베이스에 대한 CREATE TABLE 권한과 대상 서버의 대상 소유자 스키마에 대한 ALTER 권한이 있어야 함

-dt 옵션을 사용하려면 db_owner 고정 데이터베이스 역할의 멤버이거나 적어도 대상 서버의 대상 소유자 스키마에 대한 ALTER 권한이 있어야 함.

  • 사용 법

 

"C:\Program Files\Microsoft SQL Server\90\COM\tablediff.exe" -sourceserver server1 -sourcedatabase test -sourcetable table1 -destinationserver server1 -destinationdatabase test -destinationtable table2

 

 

  • 결과

Microsoft (R) SQL Server Replication Diff Tool
Copyright (C) 1988-2005 Microsoft Corporation. All rights reserved.

User-specified agent parameter values:
-sourceserver server1
-sourcedatabase test
-sourcetable table1
-destinationserver server2
-destinationdatabase test
-destinationtable table2

Table [test].[dbo].[table1] on server1 and Table [test].[dbo].[table2] on server1 have 3 differences.
Err PersonID
Mismatch 1
Dest. only 2
Src. only 3
The requested operation took 0.4375 seconds.



출처: http://ceusee.tistory.com/187 [Bora.Choi]



http://www.sqler.com/104926


SQL2005의 여러가지 유틸리티중 하나인 TableDiff 유틸리티에 대해서 알아봅시다.

이 유틸리티는 복제된 테이블의 내용 비교를 목적으로 만들어 졌으나, 일반 테이블들의 비교 용도로 활용할 수 있습니
다.

아래 샘플 예제로써, TableDiff 유틸리티의 사용법에 대해 알아보도록하죠.


/*
테이블 비교를 위해서 testDB1과 testDB2를 생성하고, TBL_TEST테이블을 각각 만들어서 샘플데이타를 입력합니다.
*/
USE [master]
GO

--샘플DB1생성
IF EXISTS (SELECT name FROM sys.databases WHERE name = N"testDB1")
DROP DATABASE [testDB1]
go
create database testDB1
go
use testDB1
go
--샘플테이블 생성
create table TBL_TEST (id int primary key, name varchar(100),age int)
go
insert into TBL_TEST select 1,"박지성",27
insert into TBL_TEST select 2,"이영표",29
insert into TBL_TEST select 3,"설기현",28
insert into TBL_TEST select 4,"최홍만",26
go
USE [master]
GO
--샘플DB2생성
IF EXISTS (SELECT name FROM sys.databases WHERE name = N"testDB2")
DROP DATABASE [testDB2]
go
Create database testDB2
go
use testDB2
go
--샘플테이블 생성
create table TBL_TEST (id int primary key, name varchar(100),age int)
go

--일부 데이타를 달리해서 입력함.
insert into TBL_TEST select 1,"박죄송",27
insert into TBL_TEST select 2,"이영표",29
insert into TBL_TEST select 3,"설기현",28
insert into TBL_TEST select 4,"최형만",36
insert into TBL_TEST select 5,"김창식",46
go

/* 테스트를 위한 준비가 끝났으니 본격적으로 TableDiff유틸리티를 써보도록 하죠. */
도스 cmd창을 띄우시고 TableDiff유틸리티가 있는 폴더로 이동합니다.

C:\>cd "C:\Program Files\Microsoft SQL Server\90\COM"


해당 경로에서 아래의 명령을 실행합니다.

C:\Program Files\Microsoft SQL Server\90\COM>tablediff -sourceserver "SQL서버명" -
sourcedatabase "testDB1" -sourcetable "TBL_TEST" -destinationserver "SQL서버명" -destinat
iondatabase "testDB2" -destinationtable "TBL_TEST"

이렇게 실행하시면, 일치하지 않는 부분에 대한 요약정보를 출력해줍니다.

User-specified agent parameter values:
-sourceserver jkkim
-sourcedatabase testDB1
-sourcetable tbl_test
-destinationserver jkkim
-destinationdatabase testDB2
-destinationtable tbl_test

Table [testDB1].[dbo].[tbl_test] on jkkim and Table [testDB2].[dbo].[tbl_test] o
n jkkim have 3 differences.
Err id
Mismatch 1
Mismatch 4
Dest. only 5
The requested operation took 0.1875 seconds.

추가로, 좀전에 실행했던 명령에 -f 옵션을 추가해서 실행해보도록하죠.

C:\Program Files\Microsoft SQL Server\90\COM>tablediff -sourceserver "SQL서버명" -
sourcedatabase "testDB1" -sourcetable "TBL_TEST" -destinationserver "SQL서버명" -destinat
iondatabase "testDB2" -destinationtable "TBL_TEST" -f "c:\diff"

그럼 아래 결과와 함께 c:\diff.sql이라는 파일이 생성됩니다.

User-specified agent parameter values:
-sourceserver jkkim
-sourcedatabase testDB1
-sourcetable tbl_test
-destinationserver jkkim
-destinationdatabase testDB2
-destinationtable tbl_test
-f c:\mydiff

Table [testDB1].[dbo].[tbl_test] on jkkim and Table [testDB2].[dbo].[tbl_test] o
n jkkim have 3 differences.
Fix SQL written to c:\mydiff.sql.
Err id Col
Mismatch 1 name
Mismatch 4 age name
Dest. only 5
The requested operation took 0.203125 seconds.

c:\diff.sql 파일을 메모장에서 열어봅니다.
아래와 같은 DML구문이 자동으로 생성되었습니다.
바로, 원본테이블과 똑같이 맞추기 위해서 적용해야 할 구문입니다.
-- Host: jkkim
-- Database: [testDB2]
-- Table: [dbo].[tbl_test]
UPDATE [dbo].[tbl_test] SET [name]="박지성" WHERE [id] = 1
UPDATE [dbo].[tbl_test] SET [age]=26,[name]="최홍만" WHERE [id] = 4
DELETE FROM [dbo].[tbl_test] WHERE [id] = 5


이외에도 tablediff에는 다양한 파라미터가 존재합니다.
tablediff -? 명령으로 도움말을 확인하실 수 있습니다.

C:\Program Files\Microsoft SQL Server\90\COM>tablediff -?
Microsoft (R) SQL Server Replication Diff Tool
Copyright (C) 1988-2005 Microsoft Corporation. All rights reserved.

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 (Default 8000)
-strict Strict compare of source and destination schema
-rc Number of retries
-ri Retry interval


감사합니다.

참고자료 : http://www.databasejournal.com/features/mssql/article.php/3594926



https://stackoverflow.com/questions/3881111/where-can-i-find-tablediff-exe


On Windows with 64-bit SQL Server 2014, tablediff.exe is located here:

C:\Program Files\Microsoft SQL Server\120\COM\tablediff.exe

  • Replace 120 with 110 for SQL Server 2012, 100 for SQL Server 2008, etc.
  • For 32-bit version of SQL Server running on 64-bit Windows, replace Program Files with Program Files (x86)


---------------------------------------------------------------------------------------------------------------------------------------


https://docs.microsoft.com/en-us/sql/tools/tablediff-utility


tablediff Utility

The tablediff utility is used to compare the data in two tables for non-convergence, and is particularly useful for troubleshooting non-convergence in a replication topology. This utility can be used from the command prompt or in a batch file to perform the following tasks: +

  • A row by row comparison between a source table in an instance of Microsoft SQL Server acting as a replication Publisher and the destination table at one or more instances of SQL Server acting as replication Subscribers.

  • Perform a fast comparison by only comparing row counts and schema.

  • Perform column-level comparisons.

  • Generate a Transact-SQL script to fix discrepancies at the destination server to bring the source and destination tables into convergence.

  • Log results to an output file or into a table in the destination database.

  • +

Syntax


tablediff   
[ -? ] |   
{  
        -sourceserver source_server_name[\instance_name]  
        -sourcedatabase source_database  
        -sourcetable source_table_name   
    [ -sourceschema source_schema_name ]  
    [ -sourcepassword source_password ]  
    [ -sourceuser source_login ]  
    [ -sourcelocked ]  
        -destinationserver destination_server_name[\instance_name]  
        -destinationdatabase subscription_database   
        -destinationtable destination_table   
    [ -destinationschema destination_schema_name ]  
    [ -destinationpassword destination_password ]  
    [ -destinationuser destination_login ]  
    [ -destinationlocked ]  
    [ -b large_object_bytes ]   
    [ -bf number_of_statements ]   
    [ -c ]   
    [ -dt ]   
    [ -et table_name ]   
    [ -f [ file_name ] ]   
    [ -o output_file_name ]   
    [ -q ]   
    [ -rc number_of_retries ]   
    [ -ri retry_interval ]   
    [ -strict ]  
    [ -t connection_timeouts ]   
}  

Arguments

[ -? ]
Returns the list of supported parameters. +

-sourceserver source_server_name[\instance_name]
Is the name of the source server. Specify source_server_name for the default instance of SQL Server. Specify source_server_name\instance_name for a named instance of SQL Server. +

-sourcedatabase source_database
Is the name of the source database. +

-sourcetable source_table_name
Is the name of the source table being checked. +

-sourceschema source_schema_name
The schema owner of the source table. By default, the table owner is assumed to be dbo. +

-sourcepassword source_password
Is the password for the login used to connect to the source server using SQL Server Authentication. +

Important

When possible, supply security credentials at runtime. If you must store credentials in a script file, you should secure the file to prevent unauthorized access. +

-sourceuser source_login
Is the login used to connect to the source server using SQL Server Authentication. If source_login is not supplied, then Windows Authentication is used when connecting to the source server. When possible, use Windows Authentication. +

-sourcelocked
The source table is locked during the comparison using the TABLOCK and HOLDLOCK table hints. +

-destinationserver destination_server_name[\instance_name]
Is the name of the destination server. Specify destination_server_name for the default instance of SQL Server. Specify destination_server_name\instance_name for a named instance of SQL Server. +

-destinationdatabase subscription_database
Is the name of the destination database. +

-destinationtable destination_table
Is the name of the destination table. +

-destinationschema destination_schema_name
The schema owner of the destination table. By default, the table owner is assumed to be dbo. +

-destinationpassword destination_password
Is the password for the login used to connect to the destination server using SQL Server Authentication. +

Important

When possible, supply security credentials at runtime. If you must store credentials in a script file, you should secure the file to prevent unauthorized access. +

-destinationuser destination_login
Is the login used to connect to the destination server using SQL Server Authentication. If destination_login is not supplied, then Windows Authentication is used when connecting to the server. When possible, use Windows Authentication. +

-destinationlocked
The destination table is locked during the comparison using the TABLOCK and HOLDLOCK table hints. +

-b large_object_bytes
Is the number of bytes to compare for large object data type columns, which includes: text, ntext, image, varchar(max), nvarchar(max) and varbinary(max). large_object_bytes defaults to the size of the column. Any data above large_object_bytes will not be compared. +

-bf number_of_statements
Is the number of Transact-SQL statements to write to the current Transact-SQL script file when the -f option is used. When the number of Transact-SQL statements exceeds number_of_statements, a new Transact-SQL script file is created. +

-c
Compare column-level differences. +

-dt
Drop the result table specified by table_name, if the table already exists. +

-et table_name
Specifies the name of the result table to create. If this table already exists, -DT must be used or the operation will fail. +

-f [ file_name ]
Generates a Transact-SQL script to bring the table at the destination server into convergence with the table at the source server. You can optionally specify a name and path for the generated Transact-SQL script file. If file_name is not specified, the Transact-SQL script file is generated in the directory where the utility runs. +

-o output_file_name
Is the full name and path of the output file. +

-q
Perform a fast comparison by only comparing row counts and schema. +

-rc number_of_retries
Number of times that the utility retries a failed operation. +

-ri retry_interval
Interval, in seconds, to wait between retries. +

-strict
Source and destination schema are strictly compared. +

-t connection_timeouts
Sets the connection timeout period, in seconds, for connections to the source server and destination server. +

Return Value

ValueDescription
0Success
1Critical error
2Table differences

Remarks

The tablediff utility cannot be used with non- SQL Server servers. +

Tables with sql_variant data type columns are not supported. +

By default, the tablediff utility supports the following data type mappings between source and destination columns. +

Source data typeDestination data type
tinyintsmallint, int, or bigint
smallintint or bigint
intbigint
timestampvarbinary
varchar(max)text
nvarchar(max)ntext
varbinary(max)image
textvarchar(max)
ntextnvarchar(max)
imagevarbinary(max)

Use the -strict option to disallow these mappings and perform a strict validation. +

The source table in the comparison must contain at least one primary key, identity, or ROWGUID column. When you use the -strict option, the destination table must also have a primary key, identity, or ROWGUID column. +

The Transact-SQL script generated to bring the destination table into convergence does not include the following data types: +

  • varchar(max)

  • nvarchar(max)

  • varbinary(max)

  • timestamp

  • xml

  • text

  • ntext

  • image

  • +

Permissions

To compare tables, you need SELECT ALL permissions on the table objects being compared. +

To use the -et option, you must be a member of the db_owner fixed database role, or at least have CREATE TABLE permission in the subscription database and ALTER permission on the destination owner schema at the destination server. +

To use the -dt option, you must be a member of the db_owner fixed database role, or at least have ALTER permission on the destination owner schema at the destination server. +

To use the -o or -f options, you must have write permissions to the specified file directory location. +

See Also

Compare Replicated Tables for Differences (Replication Programming)






https://www.mssqltips.com/sqlservertip/1073/sql-server-tablediff-command-line-utility/


SQL Server tablediff command line utility


By:   |   Read Comments (7)   |   Related Tips: 1 | 2 | 3 | 4 | More > Comparison Data and Objects

Problem

One problem that DBAs often face is maintaining lookups tables across multiple servers or sites.  These tables could either be replicated or manually updated, but in any case sometimes the data in these tables get out of synch.  In a previous tip we talked about SQL Server comparison tools that allowed you to compare database objects or even the data itself between two different databases.  These tools are great and definitely offer a lot of advantages,  but there is a tool in SQL Server that might help as well. 

Solution

SQL Server comes with many GUI and command line tools and one new tool that is available is the tablediff command.  This utility allows you to compare the contents of one table against another table to identity if there are differences as well as create the T-SQL commands to get the tables back in synch.

The one problem with this command is that it is a command line tool, there is not GUI so writing the command is a bit tedious.  You can create a batch file and execute the batch file, but still it would be nice if there were a GUI.

The command takes a few basic parameters to run.

  • sourceserver
  • sourcedatabase
  • sourcetable
  • destinationserver
  • destinationdatabase
  • destinationtable

Here is an example command that compares two tables Table1 and Table2 in the same database.

"C:\Program Files\Microsoft SQL Server\90\COM\tablediff.exe" -sourceserver server1 -sourcedatabase test -sourcetable table1 -destinationserver server1 -destinationdatabase test -destinationtable table2

When the command is run this is the output that you get:

Microsoft (R) SQL Server Replication Diff Tool
Copyright (C) 1988-2005 Microsoft Corporation. All rights reserved.

User-specified agent parameter values:
-sourceserver server1
-sourcedatabase test
-sourcetable table1
-destinationserver server2
-destinationdatabase test
-destinationtable table2

Table [test].[dbo].[table1] on server1 and Table [test].[dbo].[table2] on server1 have 3 differences.
Err PersonID
Mismatch 1
Dest. only 2
Src. only 3
The requested operation took 0.4375 seconds.

From this basic command we can see there are differences, but it is not very helpful as to what the problem is, so to make this more useful we can use the "-et" argument to see the differences in a table.  The "et" parameter will create a table, in our case called "Difference", so we can see the differences in a table.

"C:\Program Files\Microsoft SQL Server\90\COM\tablediff.exe" -sourceserver server1  -sourcedatabase test -sourcetable table1 -destinationserver server1  -destinationdatabase test -destinationtable table2 -et Difference

When we query the table "Difference" that was created we see the following results.  As you can see this is not any more helpful then the first run.

PersonIdMSdifftool_ErrorCodeMSdifftool_ErrorDescription
10Mismatch
21Dest. only
32Src. only
NULLNULLNULL

Another option is to use the "-f" argument that will create a T-SQL script to synchronize the two tables.

"C:\Program Files\Microsoft SQL Server\90\COM\tablediff.exe" -sourceserver server1  -sourcedatabase test -sourcetable table1 -destinationserver server1 -destinationdatabase test -destinationtable table2 -et Difference -f c:\table1_differences.sql

This is the output we get from the file that is created "c:\table1_differences.sql"

-- Host: server1
-- Database: [test]
-- Table: [dbo].[table2]
UPDATE [dbo].[table2] SET [LastName]=NULL WHERE [PersonID] = 1
DELETE FROM [dbo].[table2] WHERE [PersonID] = 2
INSERT INTO [dbo].[table2] ([FirstName],[LastName],[PersonID]) VALUES ('Bob','Jones',3)

From here we can see the exact differences as well as having a script that we can run against Table2 to make the contents identical to Table1.

Next Steps
  • This is a brief introduction of this new tool and how it can be used, check out tablediff in books online for additional options
  • Add this new tool to your SQL toolbox and deploy where you need to keep certain tables in synch.
  • In order to run this command you need to have primary keys setup on both tables.


Last Update: