良许Linux教程网 干货合集 通过SQLAgent实现Oracle与SQL Server表数据同步

通过SQLAgent实现Oracle与SQL Server表数据同步

将SQLServer2008中的某些表同步到Oracle数据库中,不同数据库类型之间的数据同步我们可以使用链接服务器和SQLAgent来实现

image-20220222081744164

实例1:

SQLServer2008有一个表employ_epl是需要同步到一个EHR系统中(Oracle11g),实现数据库的同步步骤如下:

1.在Oracle中建立对应的employ_epl表,需要同步哪些字段我们就建那些字段到Oracle表中。 注意:Oracle的数据类型和SQLServer的数据类型是不一样的,需要进行转换

–查看SQLServer和其他数据库系统的数据类型对应关系 –SQL转Oracle的类型对应

SELECT *FROM msdb.dbo.MSdatatype_mappings

–详细得显示了各个数据库系统的类型对应

SELECT *FROM msdb.dbo.sysdatatypemappings

2.建立链接服务器 将Oracle系统作为SQL Server的链接服务器加入到SQL Server中。

http://www.linuxidc.com/Linux/2016-04/130574.htm

3.使用SQL语句通过链接服务器将SQLServer数据写入Oracle中

DELETE FROM TESTORACLE..SCOTT.EMPLOY_EPL
insert into TESTORACLE..SCOTT.EMPLOY_EPL
select * from employ_epl

–查看Oracle数据库中是否已经有数据了。

select * from TESTORACLE..SCOTT.EMPLOY_EPL

4.建立SQLAgent,将以上同步SQL语句作为执行语句,每天定时同步。

实例2:依靠”作业”定时调度存储过程来操作数据,增,删,改,全在里面,结合触发器,游标来实现,关于作业调度,使用了5秒运行一次来实行”秒级作业”,这样基本就算比较快的”同步”

–1.准备一个新表 –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.用一个视图”封装”了一下链接服务器下的一张表

create view v_ora_employ
as
 --TESTORACLE链接服务器名
 select * from TESTORACLE..SCOTT.EMPLOY_EPL

–3.SQL2008表employ_epl建立触发器,用表EmployLastRec_Sql记录下操作的标识 –modiid等于1为insert,2为delete,3为update,字段isexec标识该条记录是否已处理,0为未执行的,1为已执行的

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.创建存储过程进行导数到ORACLE –使用游标逐行提取EmployLastRec_Sql记录,根据modiid判断不同的数据操作,该条记录处理完毕后把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标识该条记录是否已处理,0为未执行的,1为已执行的

if not exists(select * from EmployLastRec_Sql where IsExec=0)
 begin  
   truncate table EmployLastRec_Sql----不存在未执行的,则清空表
 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为未执行的,1为已执行的

 open cur_sql
 fetch next from cur_sql into @modiid,@employID,@employName,@sex
 while @@fetch_status=0
 begin
   if (@modiid=1) --插入
   begin
     ----将数据插入到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.调用该存储过程的作业,实现5秒执行一次该存储过程,做到5秒数据同步。 –先建一个一分钟运行一次的作业,然后在”步骤”的脚本中这样写:

DECLARE @dt datetime
SET @dt = DATEADD(minute, -1, GETDATE())
--select @dt
WHILE @dt '00:00:05' -- 等待5秒, 根据你的需要设置即可
END

以上就是良许教程网为各位朋友分享的Linu系统相关内容。想要了解更多Linux相关知识记得关注公众号“良许Linux”,或扫描下方二维码进行关注,更多干货等着你 !

img
本文由 良许Linux教程网 发布,可自由转载、引用,但需署名作者且注明文章出处。如转载至微信公众号,请在文末添加作者公众号二维码。
良许

作者: 良许

良许,世界500强企业Linux开发工程师,公众号【良许Linux】的作者,全网拥有超30W粉丝。个人标签:创业者,CSDN学院讲师,副业达人,流量玩家,摄影爱好者。
上一篇
下一篇

发表评论

联系我们

联系我们

公众号:良许Linux

在线咨询: QQ交谈

邮箱: yychuyu@163.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

关注微博
返回顶部