记一次异地数据库表同步

此博客给出一段数据库存储过程代码,用于实现表数据同步。先判断中间临时表是否存在,不存在则根据源表创建并同步数据到目标表;若存在,则关联源表和临时表,查出要插入的数据并插入目标表,最后提交事务。

create or replace procedure syntest Authid Current_User is
is_exit number;
code_count number;
code varchar2(10);
begin
  --1.判断是否存在中间临时表b,不存在则根据源表a创建中间表b,并把全部数据同步到目标表kms_dot
     execute immediate 'select count(*) as is_exit from user_tables where table_name = ''T_ALL_AREA''' into is_exit;
     --dbms_output.put_line(is_exit);
     if  (is_exit = 0) then
       execute immediate 'create table t_all_area as select * from t_all_area@cssp';
       for i in (select * from t_all_area) loop
         code := i.id;
         select count(*) into code_count from kms_dot where locationcode = code;
         if (code_count = 0) then
           insert into kms_dot_syntest(LOCATIONCODE,LOCATIONNAME,STATUS,OPERUSER,OPRTDATETIME,OPERTIME)
           values(i.id,i.name,'1','万达数据库录入',sysdate,sysdate);
         end if;
       end loop;
     --2.关联源表a和中间临时表b,查出要插入的数据并插入到目标表kms_dot中
     else
       for i in (select a.* from t_all_area@cssp a left join t_all_area b on a.id = b.id where b.id is null) loop
         code := i.id;
         select count(*) into code_count from kms_dot where locationcode = code;
         if (code_count = 0) then
           insert into kms_dot_syntest(LOCATIONCODE,LOCATIONNAME,STATUS,OPERUSER,OPRTDATETIME,OPERTIME)
           values(i.id,i.name,'1','万达数据库录入',sysdate,sysdate);
         end if;
       end loop;
     end if;
     COMMIT;
end syntest;

转载于:https://www.cnblogs.com/zsqfightyourway/p/10937733.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值