Oracle与SQL Server实现表数据同步
将SQLServer2008中的某些表同步到Oracle数据库中,不同数据库类型之间的数据同步我们可以使用链接服务器和SQLAgent来实现。
实例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
--SQL转Oracle的类型对应
SELECT *FROM msdb.dbo.MSdatatype_mappings
--详细得显示了各个数据库系统的类型对应
SELECT *FROM msdb.dbo.sysdatatypemappings
2.建立链接服务器
将Oracle系统作为SQL Server的链接服务器加入到SQL Server中。
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
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](
[id] [int] IDENTITY(1,1) NOT NULL,
[modiid] [int] NULL,
[IsExec] [int] NULL,
[epl_employID] [varchar](30) NULL,
[epl_employName] [varchar](30) 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
--SqlServer表EmployLastRec_Sql用于记录employ_epl表的增删改记录
CREATE TABLE [dbo].[EmployLastRec_Sql](
[id] [int] IDENTITY(1,1) NOT NULL,
[modiid] [int] NULL,
[IsExec] [int] NULL,
[epl_employID] [varchar](30) NULL,
[epl_employName] [varchar](30) 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
--使用游标逐行提取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 < GETDATE()
BEGIN
EXEC sp_EmployLastRec_Sql
WAITFOR DELAY '00:00:05' -- 等待5秒, 根据你的需要设置即可
END
--先建一个一分钟运行一次的作业,然后在"步骤"的脚本中这样写:
DECLARE @dt datetime
SET @dt = DATEADD(minute, -1, GETDATE())
--select @dt
WHILE @dt < GETDATE()
BEGIN
EXEC sp_EmployLastRec_Sql
WAITFOR DELAY '00:00:05' -- 等待5秒, 根据你的需要设置即可
END
相关推荐
周嘉笙 2020-11-09
zhuzhufxz 2020-09-16
lklong 2020-11-22
oraclemch 2020-11-06
shilukun 2020-10-10
bfcady 2020-08-16
Hody 2020-08-16
FightFourEggs 2020-08-16
数据库设计 2020-08-16
yanghuatong 2020-08-16
dbasunny 2020-08-16
罗罗 2020-08-16
ihuaqiang 2020-08-16
choice0 2020-07-30
娜娜 2020-07-28
solarspot 2020-07-28
踩风火轮的乌龟 2020-07-26
娜娜 2020-07-20
xwb 2020-07-19
娜娜 2020-07-18
流云追风 2020-07-04
dataminer 2020-06-25
娜娜 2020-06-22
zhangchaoming 2020-06-21