记一次异地数据库表同步

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;

猜你喜欢

转载自www.cnblogs.com/zsqfightyourway/p/10937733.html