将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 .(
IDENTITY(1,1) NOT NULL,
NULL,
NULL,
(30) NULL,
(30) NULL,
NULL,
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 ,,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 ,,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 ,,epl_employID,epl_employName,epl_Sex from deleted
--SqlServer表EmployLastRec_Sql用于记录employ_epl表的增删改记录
CREATE TABLE .(
IDENTITY(1,1) NOT NULL,
NULL,
NULL,
(30) NULL,
(30) NULL,
NULL,
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 ,,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 ,,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 ,,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 --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 --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