說到同步,其實是靠"作業"定時調度預存程序來操作資料,增,刪,改,全在裡面,結合觸發器,遊標來實現,關於作業調度,我使用了5秒運行一次來實行"秒級作業",這樣基本就算比較快的"同步" 我做的是sql server往oracle端同步,先在sql server上建立往oracle端的連結的伺服器,我用一個視圖"封裝"了一下連結的伺服器下的一張表 create view v_ora_PUBLISHLASTREC as select * from ORACLEDB..ROADSMS.PUBLISHLASTREC //ORACLEDB連結的伺服器名,ROADSMS為資料表空間名,PUBLISHLASTREC 為資料表名 然後我們分別在sql server 要同步的表上建立,insert,delete,update觸發器 指令碼如下: --說明:modiid等於1為insert,2為delete,3為update create trigger trg_PUBLISHLASTREC_insert on PUBLISHLASTREC for insert as insert into dbo.PublishLastRec_SQL(modiid,SignalGUID,AreaNo,SignalNote,AreaNote,PublishRoadStatus,PublishTime,IsExec) select '1',SignalGUID,AreaNo,SignalNote,AreaNote,PublishRoadStatus,PublishTime,'0' from inserted create trigger trg_PUBLISHLASTREC_update on PUBLISHLASTREC for update as insert into dbo.PublishLastRec_SQL(modiid,SignalGUID,AreaNo,SignalNote,AreaNote,PublishRoadStatus,PublishTime,IsExec) select '3',SignalGUID,AreaNo,SignalNote,AreaNote,PublishRoadStatus,PublishTime,'0' from inserted create trigger trg_PUBLISHLASTREC_delete on PUBLISHLASTREC for delete as insert into dbo.PublishLastRec_SQL(modiid,SignalGUID,AreaNo,SignalNote,AreaNote,PublishRoadStatus,PublishTime,IsExec) select '2',SignalGUID,AreaNo,SignalNote,AreaNote,PublishRoadStatus,PublishTime,'0' from deleted 所有的操作都是把各幻表的資料插入到一張表中,上面統一插入的表為PublishLastRec_SQL,記錄下操作的標識,以標識該條記錄是插入,刪除,還是修改,modiid等於1為insert,2為delete,3為update,欄位isexec標識該條記錄是否已處理,0為未執行的,1為已執行的 接著就是最關鍵的一步,預存程序 指令碼如下 ALTER proc pro_PublishLastRec_Sql as declare @modiid int declare @signalguid int declare @areano numeric(1,0) declare @signalnote varchar(50) declare @areanote varchar(50) declare @publishroadstatus varchar(20) declare @publishtime varchar(50) if not exists(select * from PublishLastRec_SQL where IsExec=0) begin truncate table PublishLastRec_SQL return end declare cur_sql cursor for select modiid,SignalGUID,AreaNo,SignalNote,AreaNote,PublishRoadStatus,PublishTime from PublishLastRec_SQL where IsExec=0 order by [id]--IsExec 0為未執行的,1為已執行的 open cur_sql fetch next from cur_sql into @modiid,@SignalGUID,@AreaNo,@SignalNote,@AreaNote,@PublishRoadStatus,@PublishTime while @@fetch_status=0 begin if (@modiid=1) --插入 begin insert into v_ora_PUBLISHLASTREC(SignalGUID,AreaNo,SignalNote,AreaNote,PublishRoadStatus,PublishTime) values(@SignalGUID,@AreaNo,@SignalNote,@AreaNote,@PublishRoadStatus,@PublishTime) end if (@modiid=2) --刪除 begin delete from v_ora_PUBLISHLASTREC where SignalGUID=@SignalGUID and AreaNo=@AreaNo end if (@modiid=3) --修改 begin update v_ora_PUBLISHLASTREC set SignalNote=@SignalNote,AreaNote=@AreaNote,PublishRoadStatus=@PublishRoadStatus, PublishTime=@PublishTime where SignalGUID=@SignalGUID and AreaNo=@AreaNo end update PublishLastRec_SQL set IsExec=1 where current of cur_sql fetch next from cur_sql into @modiid,@SignalGUID,@AreaNo,@SignalNote,@AreaNote,@PublishRoadStatus,@PublishTime end deallocate cur_sql 該預存程序使用遊標逐行提取PublishLastRec_Sql記錄,根據modiid判斷不同的資料操作,該條記錄處理完畢後把isexec欄位更新為1. 最後是調用該預存程序的作業,我們先建一個一分鐘運行一次的作業,然後在"步驟"的指令碼中這樣寫: DECLARE @dt datetime SET @dt = DATEADD(minute, -1, GETDATE()) WHILE @dt < GETDATE() BEGIN EXEC pro_PublishLastRec_Sql --這裡pro_PublishLastRec_Sql 為你要作業執行的預存程序 WAITFOR DELAY '00:00:05' -- 等待5秒, 根據你的需要設定即可 END 如此我們便實現5秒執行一次該預存程序,做到5秒同步,^_^,please enjoy! |