脚本设计 存储过程的 类型, Exception处理,循环,判断,游标,动态sql
CREATE OR REPLACE FUNCTION proc_batch_tmp2ods(start_time timestamp without time zone, end_time timestamp without time zone, source character varying)
RETURNS void
LANGUAGE plpgsql
NOT FENCED NOT SHIPPABLE
AS $$
declare
s_time DATE;
e_time DATE;
vs_time DATE;
ve_time DATE;
e_type INTEGER:=0;
v_table_name VARCHAR2(200);
v_sql VARCHAR2(4000);
v_sql2 VARCHAR2(4000);
status INTEGER;
exp_data_range exception;
type table_info is record(
table_name dc_conf_table_info.table_name%type,
con_columns dc_conf_table_info.con_columns%type,
con_key dc_conf_table_info.con_key%type,
del_condition dc_conf_table_info.del_condition%type
);
v_table_info table_info;
cursor table_info_cursor is select table_name,con_columns,con_key,del_condition from dc_conf_table_info where source_system=source;
begin
for v in table_info_cursor loop
begin
v_table_name=v.table_name;
select exe_status into status from dc_conf_table_info where table_name=v.table_name;
if(status <> 1) then
raise exp_data_range;
end if;
select now() into s_time from dual;
v_sql='delete from '||v.table_name||' a where exists (select 1 from '||v_table_name||'_tmp b where crt_date>='||chr(39)||start_time||chr(39)||' and crt_date<'||chr(39)||end_time||chr(39)||' and ' ||v.del_condition||')';
execute immediate v_sql;
select now() into e_time from dual;
e_type=1;
insert into ds_batch_gaussdb_log select s_time,v.table_name,'delete',e_type,null,start_time,end_time,e_time,v_sql,'ODS',source from dual;
update dc_conf_table_info set exe_status='2' where table_name=v.table_name;
begin
select exe_status into status from dc_conf_table_info where table_name=v.table_name;
if(status <> 2) then
raise exp_data_range;
end if;
select now() into vs_time from dual;
v_sql2='insert into '||v.table_name ||' select '|| v.con_columns ||' from ( select *, row_number() over(partition by '|| v.con_key ||' order by senddate,sequenceno desc ) rn from '||
v_table_name||'_tmp where crt_date>='||chr(39)||start_time||chr(39)||' and crt_date<'||chr(39)||end_time||chr(39)||') a where a.rn=1';
execute immediate v_sql2;
select now() into ve_time from dual;
e_type=2;
insert into ds_batch_gaussdb_log select vs_time,v.table_name,'insert',e_type,null,start_time,end_time,ve_time,v_sql2,'ODS',source from dual;
update dc_conf_table_info set exe_status='1' where table_name=v.table_name;
EXCEPTION
when exp_data_range then
e_type=-2;
insert into ds_batch_gaussdb_log select s_time,v_table_name,'insert',e_type,sqlerrm,start_time,end_time,e_time,v_sql2,'ODS',source from dual;
when others then
e_type=-2;
insert into ds_batch_gaussdb_log select s_time,v_table_name,'insert',e_type,sqlerrm,start_time,end_time,e_time,v_sql2,'ODS',source from dual;
end;
EXCEPTION
when exp_data_range then
e_type=-1;
insert into ds_batch_gaussdb_log select s_time,v_table_name,'delete',e_type,sqlerrm,start_time,end_time,e_time,v_sql,'ODS',source from dual;
when others then
e_type=-1;
insert into ds_batch_gaussdb_log select s_time,v_table_name,'delete',e_type,sqlerrm,start_time,end_time,e_time,v_sql,'ODS',source from dual;
end;
end loop;
end$$
/