人大金仓 PostgreSQL 触发器同步记录数据到另一张表

创建触发器

sql 复制代码
CREATE TRIGGER sync_tz_ly_event_trigger AFTER INSERT 
OR UPDATE 
	OR DELETE ON tz_ly_event FOR EACH ROW
	EXECUTE FUNCTION sync_tz_ly_event_records ( );

创建触发函数

sql 复制代码
CREATE OR REPLACE FUNCTION "sync_tz_ly_event_records"()
  RETURNS "pg_catalog"."trigger" AS $BODY$ BEGIN
	IF
		TG_OP = 'INSERT' THEN
			INSERT INTO training_course_new ( record_id,class_name )
		VALUES
			( NEW.id,NEW.title );
		ELSIF TG_OP = 'UPDATE' THEN
			UPDATE training_course_new 
			SET class_name = NEW.title 
			WHERE
				record_id = CAST(OLD.id AS TEXT);
		ELSIF TG_OP = 'DELETE' THEN
				DELETE 
				FROM
					training_course_new 
				WHERE
					record_id = CAST(OLD.id AS TEXT);
			END IF;
			RETURN NULL;
		END;
	$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
sql 复制代码
CREATE OR REPLACE FUNCTION "sync_tz_ly_event_records"()
  RETURNS "pg_catalog"."trigger" AS $BODY$ BEGIN
	IF
		TG_OP = 'INSERT' THEN-- 		同心迁移活动表
			INSERT INTO training_course_new (
				record_id,
				class_name,
				dept_name,
				evt_time,
				address,
				evt_scale,
				train_man_num,
				CONTENT,
				poster_code,
				reg_bgn_date,
				reg_end_date,
				area_code,
				DOMAIN,
				publish_status,
				audit_by,
				audit_date,
				audit_comment,
-- 	因数据缺少,使用报名时间作为活动时间
				start_time,
				end_time,
				is_apply,
				ly_user_id,
				create_user,
				create_time,
				modify_user,
				modify_time,
				sort,
-- 	活动类型
				TYPE,
--  截止时间
				expire_time,
-- 	报道时间为活动开始时间
				report_time 
			)
		VALUES
			(
				NEW.ID,
				NEW.title,
				NEW.host_unit,
				NEW.evt_time,
				NEW.evt_address,
				NEW.evt_scale,
				NEW.reg_max_num,
				NEW.description,
				NEW.poster_url,
				NEW.reg_bgn_date,
				NEW.reg_end_date,
				NEW.area_code,
				NEW.DOMAIN,
				NEW.STATE,
				NEW.audit_by,
				NEW.audit_date,
				NEW.audit_comment,
-- 	因数据缺少,使用报名时间作为活动时间
-- bgn_date,
-- end_date,
				NEW.reg_bgn_date,
				NEW.reg_end_date,
				NEW.is_apply,
				NEW.ly_user_id,
				NEW.created_by,
				NEW.created_date,
				NEW.last_modified_by,
				NEW.last_modified_date,
				NEW.sort,
-- 	活动类型
				2,
-- 截止时间
				NEW.reg_end_date,
-- 	因数据缺少,报道时间,用报名结束时间
				NEW.reg_end_date 
			);
-- 			更新create_org_id,create_org_name字段 ,创建人可见改为部门可见
		UPDATE training_course_new tcn 
		SET tcn.create_org_id = dept.dept_id,
		tcn.create_org_name = dept.dept_name 
		FROM
			training_course_new course
			LEFT JOIN sys_users users ON users."id" = course.create_user
			LEFT JOIN sys_dept dept ON dept.dept_id = users.org_id 
		WHERE
			tcn.record_id = course.record_id 
			AND tcn.record_id = CAST ( NEW.ID AS TEXT );
-- 活动新闻拆分
		INSERT INTO training_course_new_news ( training_course_id, is_news, news_content, news_state, news_date, news_author )
		VALUES
			(
				NEW.ID,
				NEW.is_news,
				NEW.news_content,
			CASE
					
					WHEN NEW.news_state = 0 THEN
					2 
					WHEN NEW.news_state = 1 THEN
					4 ELSE NULL 
				END,
				NEW.news_date,
				NEW.news_author 
			);
		ELSIF TG_OP = 'UPDATE' THEN
			UPDATE training_course_new 
			SET class_name = NEW.title,
			dept_name = NEW.host_unit,
			evt_time = NEW.evt_time,
			address = NEW.evt_address,
			evt_scale = NEW.evt_scale,
			train_man_num = NEW.reg_max_num,
			CONTENT = NEW.description,
			poster_code = NEW.poster_url,
			reg_bgn_date = NEW.reg_bgn_date,
			reg_end_date = NEW.reg_end_date,
			area_code = NEW.area_code,
			DOMAIN = NEW.DOMAIN,
			publish_status = NEW.STATE,
			audit_by = NEW.audit_by,
			audit_date = NEW.audit_date,
			audit_comment = NEW.audit_comment,
			start_time = NEW.reg_bgn_date,
			end_time = NEW.reg_end_date,
			is_apply = NEW.is_apply,
			ly_user_id = NEW.ly_user_id,
			create_user = NEW.created_by,
			create_time = NEW.created_date,
			modify_user = NEW.last_modified_by,
			modify_time = NEW.last_modified_date,
			sort = NEW.sort,
			TYPE = 2,
			expire_time = NEW.reg_end_date,
			report_time = NEW.reg_end_date 
			WHERE
				record_id = CAST ( OLD.ID AS TEXT );
-- 			新闻表更新
			UPDATE training_course_new_news 
			SET is_news = NEW.is_news,
			news_content = NEW.news_content,
			news_state =
			CASE
					
					WHEN NEW.news_state = 0 THEN
					2 
					WHEN NEW.news_state = 1 THEN
					4 ELSE NULL 
				END,
				news_date = NEW.news_date,
				news_author = NEW.news_author 
			WHERE
				training_course_id = CAST ( OLD.ID AS TEXT );
			ELSIF TG_OP = 'DELETE' THEN
				DELETE 
				FROM
					training_course_new 
				WHERE
					record_id = CAST ( OLD.ID AS TEXT );
				
			END IF;
			RETURN NULL;
			
		END;
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100
相关推荐
superman超哥13 分钟前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba
engchina1 小时前
Neo4j 和 Python 初学者指南:如何使用可选关系匹配优化 Cypher 查询
数据库·python·neo4j
engchina1 小时前
使用 Cypher 查询语言在 Neo4j 中查找最短路径
数据库·neo4j
尘浮生1 小时前
Java项目实战II基于Spring Boot的光影视频平台(开发文档+数据库+源码)
java·开发语言·数据库·spring boot·后端·maven·intellij-idea
威哥爱编程1 小时前
SQL Server 数据太多如何优化
数据库·sql·sqlserver
小华同学ai1 小时前
AJ-Report:一款开源且非常强大的数据可视化大屏和报表工具
数据库·信息可视化·开源
Acrelhuang2 小时前
安科瑞5G基站直流叠光监控系统-安科瑞黄安南
大数据·数据库·数据仓库·物联网
十叶知秋2 小时前
【jmeter】jmeter的线程组功能的详细介绍
数据库·jmeter·性能测试
瓜牛_gn4 小时前
mysql特性
数据库·mysql
奶糖趣多多5 小时前
Redis知识点
数据库·redis·缓存