1.为了防止误用,设置了一个参数operation,必须为insert时才生效;
2.在新增操作过程中,如果抛出异常中断,会回滚。
CREATE OR REPLACE FUNCTION fun_insert_into_tb_sys_vul_event2(operation text) RETURNS void AS $$ BEGIN IF operation != 'insert' THEN RAISE EXCEPTION '参数错误!参数应该为insert!'; RETURN ; else -- 为了避免重复录入,每次运行前先清空sys_vul_event表 delete from sys_vul_event2; --RAISE EXCEPTION '删除失败!'; --CVE披露事件 type=1 insert into sys_vul_event2(sys_vul_id, event_date, type) select id, date_exposure, 1 as type from sys_vul where date_exposure is not NULL; --Google bulletin公开事件 type=2 insert into sys_vul_event2(sys_vul_id, event_date, source, type) select id, date_bulletin, source, 2 as type from sys_vul where date_bulletin is not NULL; --提供修复patch事件 type=3 insert into sys_vul_event2(sys_vul_id, event_date, type) select id, date_fixed, 3 as type from sys_vul where date_fixed is not NULL; --poc,exp公开事件 poc type=5;exp type=6 insert into sys_vul_event2(sys_vul_id, event_date, type) select sys_vul_id, exposed_date, case when type='exp' then 6 when type='poc' then 5 end as type from security_matter; END IF; EXCEPTION WHEN others THEN RAISE EXCEPTION '(%)', SQLERRM; ROLLBACK; END; $$ LANGUAGE PLPGSQL;
rollback就相当于结束了,和return差不多。