SQL資料同步如何同步兩個資料,包括同一個SQL伺服器和不同一個SQL服務.
一般採用釋出/訂閱的方法,這個聯機幫助上說得很清楚了,不是本貼的內容.
本貼提供兩種手工同步的方法. 即時同步和定時同步.
已知的問題: 由於使用了分散式事務處理,而這個在部分情況(具體原因未查到,有很多貼子討論這個問題,都沒有找到原因)下是不能生效的. 因此,在不能正常使用的情況下,可以取消分散式事務處理 這個不影響處理,只是無法保證資料處理的完整性. #2 --即時同步兩個表的例項:
--測試環境:SQL2000,遠端主機名:xz,使用者名稱:sa,密碼:無,資料庫名:test
--建立測試表,不能用標識列做主鍵,因為不能進行正常更新 --在本機上建立測試表,遠端主機上也要做同樣的建表操作,只是不寫觸發器 if exists (select * from dbo.sysobjects where id = object_id(N'[test]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [test]
create table test(id int not null constraint PK_test primary key ,name varchar(10)) go
--建立同步的觸發器 create trigger t_test on test for insert,update,delete as set XACT_ABORT on --啟動遠端伺服器的MSDTC服務 exec master..xp_cmdshell 'isql /S"xz" /U"sa" /P"" /q"exec master..xp_cmdshell ''net start msdtc'',no_output"',no_output
--啟動本機的MSDTC服務 exec master..xp_cmdshell 'net start msdtc',no_output
--進行分佈事務處理,如果表用標識列做主鍵,用下面的方法 BEGIN DISTRIBUTED TRANSACTION delete from openrowset('sqloledb','xz';'sa';'',test.dbo.test) where id in(select id from deleted) insert into openrowset('sqloledb','xz';'sa';'',test.dbo.test) select * from inserted commit tran go
--插入資料測試 insert into test select 1,'aa' union all select 2,'bb' union all select 3,'c' union all select 4,'dd' union all select 5,'ab' union all select 6,'bc' union all select 7,'ddd'
--刪除資料測試 delete from test where id in(1,4,6)
--更新資料測試 update test set name=name+'_123' where id in(3,5)
--顯示測試的結果 select * from test a full join openrowset('sqloledb','xz';'sa';'',test.dbo.test) b on a.id=b.id #3 --定時同步伺服器上的資料
--例子: --測試環境,SQL Server2000,遠端伺服器名:xz,使用者名稱為:sa,無密碼,測試資料庫:test --伺服器上的表(查詢分析器連線到伺服器上建立) create table [user](id int primary key,number varchar(4),name varchar(10)) go --以下在區域網(本機操作) --本機的表,state說明:null 表示新增記錄,1 表示修改過的記錄,0 表示無變化的記錄 if exists (select * from dbo.sysobjects where id = object_id(N'[user]') and OBJECTPROPERTY(id, N'IsUserTable') = 1) drop table [user] GO create table [user](id int identity(1,1),number varchar(4),name varchar(10),state bit) go --建立觸發器,維護state欄位的值 create trigger t_state on [user] after update as update [user] set state=1 from [user] a join inserted b on a.id=b.id where a.state is not null go
--為了方便同步處理,建立連結伺服器到要同步的伺服器 --這裡的遠端伺服器名為:xz,使用者名稱為:sa,無密碼 if exists(select 1 from master..sysservers where srvname='srv_lnk') exec sp_dropserver 'srv_lnk','droplogins' go exec sp_addlinkedserver 'srv_lnk','','SQLOLEDB','xz' exec sp_addlinkedsrvlogin 'srv_lnk','false',null,'sa' go
--建立同步處理的儲存過程 if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[p_synchro]') and OBJECTPROPERTY(id, N'IsProcedure') = 1) drop procedure [dbo].[p_synchro] GO create proc p_synchro as --set XACT_ABORT on --啟動遠端伺服器的MSDTC服務 --exec master..xp_cmdshell 'isql /S"xz" /U"sa" /P"" /q"exec master..xp_cmdshell ''net start msdtc'',no_output"',no_output
--啟動本機的MSDTC服務 --exec master..xp_cmdshell 'net start msdtc',no_output
--進行分佈事務處理,如果表用標識列做主鍵,用下面的方法 --BEGIN DISTRIBUTED TRANSACTION --同步刪除的資料 delete from srv_lnk.test.dbo.[user] where id not in(select id from [user])
--同步新增的資料 insert into srv_lnk.test.dbo.[user] select id,number,name from [user] where state is null
--同步修改的資料 update srv_lnk.test.dbo.[user] set number=b.number,name=b.name from srv_lnk.test.dbo.[user] a join [user] b on a.id=b.id where b.state=1
--同步後更新本機的標誌 update [user] set state=0 where isnull(state,1)=1 --COMMIT TRAN go
--建立作業,定時執行資料同步的儲存過程 if exists(SELECT 1 from msdb..sysjobs where name='資料處理') EXECUTE msdb.dbo.sp_delete_job @job_name='資料處理' exec msdb..sp_add_job @job_name='資料處理'
--建立作業步驟 declare @sql varchar(800),@dbname varchar(250) select @sql='exec p_synchro' --資料處理的命令 ,@dbname=db_name() --執行資料處理的資料庫名
exec msdb..sp_add_jobstep @job_name='資料處理', @step_name = '資料同步', @subsystem = 'TSQL', @database_name=@dbname, @command = @sql, @retry_attempts = 5, --重試次數 @retry_interval = 5 --重試間隔
--建立排程 EXEC msdb..sp_add_jobschedule @job_name = '資料處理', @name = '時間安排', @freq_type = 4, --每天 @freq_interval = 1, --每天執行一次 @active_start_time = 00000 --0點執行 go |