Gaussdb 存储过程

脚本设计 存储过程的 类型, 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);

---数据库表状态 1 可删除 2 可添加
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
	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=-1
			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
	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$$
/

你可能感兴趣的:(数据库,数据库,sql,服务器)