新聞中心
將SQLServer2008中的某些表同步到Oracle數(shù)據(jù)庫(kù)中,不同數(shù)據(jù)庫(kù)類(lèi)型之間的數(shù)據(jù)同步我們可以使用鏈接服務(wù)器和SQLAgent來(lái)實(shí)現(xiàn)

創(chuàng)新互聯(lián)于2013年創(chuàng)立,是專(zhuān)業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目網(wǎng)站設(shè)計(jì)、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元陽(yáng)朔做網(wǎng)站,已為上家服務(wù),為陽(yáng)朔各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108
實(shí)例1:
SQLServer2008有一個(gè)表employ_epl是需要同步到一個(gè)EHR系統(tǒng)中(Oracle11g),實(shí)現(xiàn)數(shù)據(jù)庫(kù)的同步步驟如下:
1.在Oracle中建立對(duì)應(yīng)的employ_epl表,需要同步哪些字段我們就建那些字段到Oracle表中。 注意:Oracle的數(shù)據(jù)類(lèi)型和SQLServer的數(shù)據(jù)類(lèi)型是不一樣的,需要進(jìn)行轉(zhuǎn)換
–查看SQLServer和其他數(shù)據(jù)庫(kù)系統(tǒng)的數(shù)據(jù)類(lèi)型對(duì)應(yīng)關(guān)系 –SQL轉(zhuǎn)Oracle的類(lèi)型對(duì)應(yīng)
SELECT *FROM msdb.dbo.MSdatatype_mappings
–詳細(xì)得顯示了各個(gè)數(shù)據(jù)庫(kù)系統(tǒng)的類(lèi)型對(duì)應(yīng)
SELECT *FROM msdb.dbo.sysdatatypemappings
2.建立鏈接服務(wù)器 將Oracle系統(tǒng)作為SQL Server的鏈接服務(wù)器加入到SQL Server中。
http://www.linuxidc.com/Linux/2016-04/130574.htm
3.使用SQL語(yǔ)句通過(guò)鏈接服務(wù)器將SQLServer數(shù)據(jù)寫(xiě)入Oracle中
DELETE FROM TESTORACLE..SCOTT.EMPLOY_EPL
insert into TESTORACLE..SCOTT.EMPLOY_EPL
select * from employ_epl
–查看Oracle數(shù)據(jù)庫(kù)中是否已經(jīng)有數(shù)據(jù)了。
select * from TESTORACLE..SCOTT.EMPLOY_EPL
4.建立SQLAgent,將以上同步SQL語(yǔ)句作為執(zhí)行語(yǔ)句,每天定時(shí)同步。
實(shí)例2:依靠”作業(yè)”定時(shí)調(diào)度存儲(chǔ)過(guò)程來(lái)操作數(shù)據(jù),增,刪,改,全在里面,結(jié)合觸發(fā)器,游標(biāo)來(lái)實(shí)現(xiàn),關(guān)于作業(yè)調(diào)度,使用了5秒運(yùn)行一次來(lái)實(shí)行”秒級(jí)作業(yè)”,這樣基本就算比較快的”同步”
–1.準(zhǔn)備一個(gè)新表 –SqlServer表EmployLastRec_Sql用于記錄employ_epl表的增刪改記錄 CREATE TABLE [dbo].[EmployLastRec_Sql](https://www.zmtbox.com/tools/[id] [int] IDENTITY(1,1) NOT NULL, [modiid] [int] NULL, [IsExec] [int] NULL, [epl_employID] varchar NULL, [epl_employName] varchar NULL, [epl_Sex] [int] NULL, [epl_data] [datetime] NULL)
–2.用一個(gè)視圖”封裝”了一下鏈接服務(wù)器下的一張表
create view v_ora_employ
as
--TESTORACLE鏈接服務(wù)器名
select * from TESTORACLE..SCOTT.EMPLOY_EPL
–3.SQL2008表employ_epl建立觸發(fā)器,用表EmployLastRec_Sql記錄下操作的標(biāo)識(shí) –modiid等于1為insert,2為delete,3為update,字段isexec標(biāo)識(shí)該條記錄是否已處理,0為未執(zhí)行的,1為已執(zhí)行的
create trigger trg_employ_epl_insert on employ_epl for insert
as
insert into EmployLastRec_Sql(modiid,IsExec,epl_employID,epl_employName,epl_Sex)
select '1','0',epl_employID,epl_employName,epl_Sex from inserted
create trigger trg_employ_epl_update on employ_epl for update
as
insert into EmployLastRec_Sql(modiid,IsExec,epl_employID,epl_employName,epl_Sex)
select '3','0',epl_employID,epl_employName,epl_Sex from inserted
create trigger trg_employ_epl_delete on employ_epl for delete
as
insert into EmployLastRec_Sql(modiid,IsExec,epl_employID,epl_employName,epl_Sex)
select '2','0',epl_employID,epl_employName,epl_Sex from deleted
–4.創(chuàng)建存儲(chǔ)過(guò)程進(jìn)行導(dǎo)數(shù)到ORACLE –使用游標(biāo)逐行提取EmployLastRec_Sql記錄,根據(jù)modiid判斷不同的數(shù)據(jù)操作,該條記錄處理完畢后把isexec字段更新為1.
create proc sp_EmployLastRec_Sql
as --epl_employID,epl_employName,epl_Sex
declare @modiid int
declare @employID varchar(30)
declare @employName varchar(50)
declare @sex int
–字段IsExec標(biāo)識(shí)該條記錄是否已處理,0為未執(zhí)行的,1為已執(zhí)行的
if not exists(select * from EmployLastRec_Sql where IsExec=0)
begin
truncate table EmployLastRec_Sql----不存在未執(zhí)行的,則清空表
return
end
declare cur_sql cursor for
select modiid,epl_employID,epl_employName,epl_Sex
from EmployLastRec_Sql where IsExec=0 order by [id]--IsExec 0為未執(zhí)行的,1為已執(zhí)行的
open cur_sql
fetch next from cur_sql into @modiid,@employID,@employName,@sex
while @@fetch_status=0
begin
if (@modiid=1) --插入
begin
----將數(shù)據(jù)插入到ORACLE表中
insert into v_ora_employ(epl_employID,epl_employName,epl_Sex)values(@employID,@employName,@sex)
end
? if (@modiid=2) --刪除
? begin
? delete from v_ora_employ where epl_employID=@employID
? end
? if (@modiid=3) --修改
? begin
? update v_ora_employ set epl_employName=@employName,epl_Sex=@sex,epl_data=getdate()
? where epl_employID=@employID
? end
? update EmployLastRec_Sql set IsExec=1 where current of cur_sql
? fetch next from cur_sql into @modiid,@employID,@employName,@sex
end
deallocate cur_sql
–5.調(diào)用該存儲(chǔ)過(guò)程的作業(yè),實(shí)現(xiàn)5秒執(zhí)行一次該存儲(chǔ)過(guò)程,做到5秒數(shù)據(jù)同步。 –先建一個(gè)一分鐘運(yùn)行一次的作業(yè),然后在”步驟”的腳本中這樣寫(xiě):
DECLARE @dt datetime
SET @dt = DATEADD(minute, -1, GETDATE())
--select @dt
WHILE @dt '00:00:05' -- 等待5秒, 根據(jù)你的需要設(shè)置即可
END
當(dāng)前文章:通過(guò)SQLAgent實(shí)現(xiàn)Oracle與SQLServer表數(shù)據(jù)同步
網(wǎng)站地址:http://www.dlmjj.cn/article/dpgjhhe.html


咨詢(xún)
建站咨詢(xún)
