创建触发器
CREATE TRIGGER sync_tz_ly_event_trigger AFTER INSERT
OR UPDATE OR DELETE ON tz_ly_event FOR EACH ROWEXECUTE FUNCTION sync_tz_ly_event_records ( );
创建触发函数
CREATE OR REPLACE FUNCTION "sync_tz_ly_event_records"()RETURNS "pg_catalog"."trigger" AS $BODY$ BEGINIFTG_OP = 'INSERT' THENINSERT INTO training_course_new ( record_id,class_name )VALUES( NEW.id,NEW.title );ELSIF TG_OP = 'UPDATE' THENUPDATE training_course_new SET class_name = NEW.title WHERErecord_id = CAST(OLD.id AS TEXT);ELSIF TG_OP = 'DELETE' THENDELETE FROMtraining_course_new WHERErecord_id = CAST(OLD.id AS TEXT);END IF;RETURN NULL;END;$BODY$LANGUAGE plpgsql VOLATILECOST 100
CREATE OR REPLACE FUNCTION "sync_tz_ly_event_records"()RETURNS "pg_catalog"."trigger" AS $BODY$ BEGINIFTG_OP = 'INSERT' THENINSERT 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,
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 );
UPDATE training_course_new tcn SET tcn.create_org_id = dept.dept_id,tcn.create_org_name = dept.dept_name FROMtraining_course_new courseLEFT JOIN sys_users users ON users."id" = course.create_userLEFT JOIN sys_dept dept ON dept.dept_id = users.org_id WHEREtcn.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,CASEWHEN NEW.news_state = 0 THEN2 WHEN NEW.news_state = 1 THEN4 ELSE NULL END,NEW.news_date,NEW.news_author );ELSIF TG_OP = 'UPDATE' THENUPDATE 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 WHERErecord_id = CAST ( OLD.ID AS TEXT );
UPDATE training_course_new_news SET is_news = NEW.is_news,news_content = NEW.news_content,news_state =CASEWHEN NEW.news_state = 0 THEN2 WHEN NEW.news_state = 1 THEN4 ELSE NULL END,news_date = NEW.news_date,news_author = NEW.news_author WHEREtraining_course_id = CAST ( OLD.ID AS TEXT );ELSIF TG_OP = 'DELETE' THENDELETE FROMtraining_course_new WHERErecord_id = CAST ( OLD.ID AS TEXT );END IF;RETURN NULL;END;
$BODY$LANGUAGE plpgsql VOLATILECOST 100