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;
css