PostgreSQL新增、更新、删除触发器

-- wf_message_cfys数据更新到wf_message触发器函数
CREATE OR REPLACE FUNCTION "public"."func_wf_message_sync_trigger"()
  RETURNS TRIGGER AS $BODY$
 BEGIN
   IF TG_OP = 'INSERT' THEN
       insert into wf_message(wf_message_id,sys_role_id,wf_biz_proc_inst_id,work_item_id,act_name,proc_inst_name,arrival_date,submit_date,uri,handler_account,handler_name,approvable,commentable,approval_comment,batchable,node_code,part_code,type,status,system_code,system_name,role_name,province_code,lot_no,create_date,update_date)VALUES(NEW.id,NEW.role_id,NEW.proc_inst_id,NEW.work_item_id,NEW.act_name,NEW.proc_inst_name,NEW.arrival_time,NEW.submit_time,NEW.uri,NEW.handler_account,NEW.handler_name,NEW.approvable,NEW.commentable,NEW.approval_comment,NEW.batchable,NEW.node_code,NEW.part_code,NEW.type,NEW.status,NEW.system_code,NEW.system_name,NEW.role_name,NEW.province_code,NEW.lot_no,now(),now());
         RETURN NEW;

    ELSIF TG_OP = 'UPDATE' THEN
       update wf_message set sys_role_id=NEW.role_id,wf_biz_proc_inst_id=NEW.proc_inst_id,work_item_id=NEW.work_item_id,act_name=NEW.act_name,proc_inst_name=NEW.proc_inst_name,arrival_date=NEW.arrival_time,submit_date=NEW.submit_time,uri=NEW.uri,handler_account=NEW.handler_account,handler_name=NEW.handler_name,approvable=NEW.approvable,commentable=NEW.commentable,approval_comment=NEW.approval_comment,batchable=NEW.batchable,node_code=NEW.node_code,part_code=NEW.part_code,type=NEW.type,status=NEW.status,system_code=NEW.system_code,system_name=NEW.system_name,role_name=NEW.role_name,province_code=NEW.province_code,lot_no=NEW.lot_no,update_date=now()
         where wf_message_id=NEW.id;
         RETURN NEW;

    ELSIF TG_OP = 'DELETE'  THEN
       delete from wf_message where wf_message_id=OLD.id;
         RETURN OLD;
   END IF;
 EXCEPTION
  WHEN OTHERS THEN
    null;
    return null;
 END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100 ;

-- wf_message_cfys数据更新到wf_message触发器
CREATE TRIGGER func_wf_message_sync_trigger AFTER INSERT OR UPDATE OR DELETE ON "public"."wf_message_cfys"
FOR EACH ROW
EXECUTE PROCEDURE "public"."func_wf_message_sync_trigger"();
原文地址:https://www.cnblogs.com/fm98/p/15772177.html