1. tablediff 是什麼?
tablediff 實用工具用於比較兩個非收斂的表中的數據,它對於排除複製拓撲中的非收斂故障非常有用。
2. tablediff 用哪些用法?
1) . 在充當複製發佈服務器的 Microsoft SQL Server 實例中的源表與充當複製訂閱服務器的一個或多個 SQL Server 實例中的目標表之間進行逐行比較。
2) . 通過只比較行數和架構可以執行快速比較。
3) . 執行列級比較。
4) . 生成 Transact-SQL 腳本,用以修復目標服務器中的差異,以使源表和目標表實現收斂。
5) . 將結果記錄到輸出文件或目標數據庫的表中。
3. tablediff 語法
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 ] }
4. tablediff 注意事項
tablediff 默認存放路徑(SQL SERVER 2008) : C:\Program Files\Microsoft SQL Server\100\COM
tablediff 實用工具不能用於非SQL SERVE服務器
若要比較表,您必須要有比較表對象的 SELECT ALL 權限
5. tablediff 使用舉例
tablediff.exe -sourceserver 10.172.2.100 -sourceuser sa -sourcepassword sasa -sourcedatabase testdb3 -sourceschema dbo -sourcetable t1 -destinationserver 10.172.2.112 -destinationuser sa -destinationpassword sasa -destinationdatabase testdb1 -destinationschema dbo -destinationtable t1 -c
創建測試環境:
--SERVER :HOUYAJUN use TEST go create table person ( id int identity(1,1), name varchar(50) default('guoqiang'), [address] varchar(100) default('anhui hefei'), infro varchar(100) ) go insert into person(infro) select 'test1' union select 'test2' union select 'test3' go select * from TEST.dbo.person with(nolock) -- SERVER: HOUYAJUN\JHIDCDBS005 use RepTest go create table person ( id int identity(1,1), name varchar(50) default('guoqiang'), [address] varchar(100) default('anhui hefei'), infro varchar(100) ) go alter table RepTest.dbo.person add t1 int insert into person(infro) select 'test1' union select 'test2' union select 'test3' go 2 select * from TEST.dbo.person with(nolock)
參數 -c : 比較列差異
a.兩個表結構完全相同且數據相同
b. 我們來在HOUYAJUN\JHIDCSDBS005上面的表加10行數據
insert into RepTest.dbo.person select 't_04', 't_05' ,'t_06' go 10
再執行上面同樣的命令可以看到以下不同:
在此處我們可以看到我對B表修改的列的主鍵,我們可以根據這些可對錶進行操作。在下面會講述如此對這些數據不一致進行生動生成腳本
c. 如果我們再對錶結構進行修改
alter table RepTest.dbo.person add te01 int
再執行上述語句可以看到以下情況:
d. 在此處我們可以看到不同,但不能看到具體的不同,我們再來把剛纔多餘的數據進行刪除看看有什麼
delete from RepTest.dbo.person where id in (4 ,5 ,6 ,7 ,8 ,9 ,10,11 ,12,13)
可以看到和上面一樣的錯誤,但是沒有出現錯誤到底是什麼不同哦。
參數 -q : 通過只比較行數和架構可以執行較快速比較
和上面-c的基本是一樣。我們再來把多出的一列刪除掉,插入2條記錄看一下。
alter table reptest.dbo.person drop column te01 go insert into RepTest.dbo.person select 't_04', 't_05' ,'t_06' go 2
現在應該是表結構一樣,數據多了2個
可以看到這個是把2個表的數據行寫出來了,一個爲3,另外一個爲5,沒有指定那些不同,但是可以到出2個表是不一樣的。
參數 –f: 生成T-SQL腳本,以使目標服務器上的表與源服務器上的表實現收斂
參數 –o: 輸出文件的完整名稱和路徑。可以輸出日誌信息。
在這裏我們沒有看到錯誤信息了,因爲錯誤信息已經寫在了log裏面,而persondiff則爲對應的SQL修改語句。
log 信息爲:
Table [test].[dbo].[person] on houyajun and Table [reptest].[dbo].[person] on houyajun\jhidcdbs005 have 2 differences. Fix SQL written to d:persondiff.sql. Err id Col Dest. Only 14 Dest. Only 15 The requested operation took 0.154 seconds.
persondiff.sql 的信息爲:
-- Host: houyajun\jhidcdbs005 -- Database: [reptest] -- Table: [dbo].[person] SET IDENTITY_INSERT [dbo].[person] ON DELETE FROM [dbo].[person] WHERE [id] = 14 DELETE FROM [dbo].[person] WHERE [id] = 15 SET IDENTITY_INSERT [dbo].[person] OFF