| 導購 | 订阅 | 在线投稿
分享
 
 
 

經驗總結:SQL Server與Oracle的數據同步

來源:互聯網網民  2008-06-13 06:49:09  評論

這篇論壇文章(賽迪網技術社區)主要介紹了SQL Server與Oracle的數據同步方案及解決過程,更多內容請參考下文:

說到同步,其實是靠"作業"定時調度存儲過程來操作數據,增,刪,改,全在裏面,結合觸發器,遊標來實現,關于作業調度,我使用了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秒數據同步。

 
特别声明:以上内容(如有图片或视频亦包括在内)为网络用户发布,本站仅提供信息存储服务。
 
這篇論壇文章(賽迪網技術社區)主要介紹了SQL Server與Oracle的數據同步方案及解決過程,更多內容請參考下文: 說到同步,其實是靠"作業"定時調度存儲過程來操作數據,增,刪,改,全在裏面,結合觸發器,遊標來實現,關于作業調度,我使用了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秒數據同步。
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 
 熱帖排行
 
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有