之前工作中做数据同步用到的触发器,做了如下笔记,总结如下:
数据中心
----------------------------------学院
create or replace trigger tger_XX_YXSDWJBSJZL_ist
before insert on zfdxc.XX_YXSDWJBSJZL
for each row
begin
insert into (bmdm,bmmc,bmjb,bmlb) values(:new.dwh,:new.dwmc,1,5);
end;
/
create or replace trigger tger_XX_YXSDWJBSJZL_udt
before update on zfdxc.XX_YXSDWJBSJZL
for each row
begin
update set bmdm=:new.dwh,bmmc=:new.dwmc where bmdm=:old.dwh;
end;
/
create or replace trigger tger_XX_YXSDWJBSJZL_del
before delete on zfdxc.XX_YXSDWJBSJZL
for each row
begin
delete where bmdm=:old.dwh;
end;
/
create or replace trigger trig_xydmbtojwgl after INSERT OR DELETE OR UPDATE
of dwh,dwmc ON xx_yxsdwjbsjzl FOR EACH ROW
BEGIN
IF INSERTING THEN
insert into (xydm,xymc) values (:new.dwh,:new.dwmc);
insert into (xydm,xymc) values (:new.dwh,:new.dwmc);
ELSIF DELETING THEN
delete from where xydm=:old.dwh;
delete from where xydm=:old.dwh;
ELSIF UPDATING THEN
update set xydm=:new.dwh,xymc=:new.dwmc where xydm=:old.dwh;
update set xydm=:new.dwh,xymc=:new.dwmc where xydm=:old.dwh;
END IF;
END;
/
-----------------------------------------专业
create or replace trigger tger_jx_zyxxsjl_ist
before insert on zfdxc.jx_zyxxsjl
for each row
begin
insert into (zydm,bmdm,zymc,zyjc,zyywmc) values(:new.zyh,:new.dwh,:new.zymc,:new.zyjc,:new.zyywmc);
end;
/
create or replace trigger tger_jx_zyxxsjl_udt
before update on zfdxc.jx_zyxxsjl
for each row
begin
update set zydm=:new.zyh,bmdm=:new.dwh,zymc=:new.zymc,zyjc=:new.zyjc,zyywmc=:new.zyywmc where zydm=:old.zyh;
end;
/
create or replace trigger tger_jx_zyxxsjl_del
before delete on zfdxc.jx_zyxxsjl
for each row
begin
delete where zydm=:old.zyh;
end;
/
---------------------------------------班级
create or replace trigger tger_xx_bjsjl_ist
before insert on zfdxc.xx_bjsjl
for each row
begin
insert into (bjdm,zydm,bmdm,bjmc,nj) values (:new.bh,:new.zyh,:new.ssxydm,:new.bj,:new.nj);
end;
/
create or replace trigger tger_xx_bjsjl_udt
before update on zfdxc.xx_bjsjl
for each row
begin
update set bjdm=:new.bh,zydm=:new.zyh,bmdm=:new.ssxydm,bjmc=:new.bj,nj=:new.nj where bjdm=:old.bh;
end;
/
create or replace trigger tger_xx_bjsjl_del
before delete on zfdxc.xx_bjsjl
for each row
begin
delete where bjdm=:old.bh;
end;
/
---------教职工基础数据
create or replace trigger trig_jzgjcsjzl_jsxxb
after insert or delete or update of jgh,dwh,xm,xbm,csrq,jg,mzm,whcdm,jzglbm,zw
on jg_jzgjcsjzl for each row
declare
v_bmmc varchar2(100);
v_xb dm_gb_rdxbdm.mc%type;
v_mz varchar2(10);
v_whcdmc varchar2(10);
v_jzglbmc varchar2(10);
maxxh varchar2(100);
kyyhbid varchar2(20);
kyyhjbxxbid varchar2(20);
BEGIN
if :new.sjly='教务' then
null;
else
begin
update set seqvalue=seqvalue + cachesize where seqname='SeqYHBID';
update set seqvalue=seqvalue + cachesize where seqname='SeqYHJBXXBID';
select seqvalue into kyyhbid from where seqname='SeqYHBID';
select seqvalue into kyyhjbxxbid from where seqname='SeqYHJBXXBID';
end;
begin
select dwmc into v_bmmc from xx_yxsdwjbsjzl where dwh=:new.dwh;
exception
when others then
v_bmmc:='-9';
end;
begin
select mc into v_xb from dm_gb_rdxbdm where dm=:new.xbm;
exception
when others then
v_xb:='-9';
end;
begin
select mc into v_mz from DM_GB_ZGGMZDLMZMPXFHDM where dm=:new.mzm;
exception
when others then
v_mz:='-9';
end;
begin
select mc into v_whcdmc from DM_HB_WHCD where dm=:new.whcdm;
exception
when others then
v_whcdmc:='-9';
end;
begin
select to_char(to_number(max(yhsx)) + 1) into maxxh from where xydm=:new.dwh;
exception
when others then
maxxh:='-9';
end;
begin
select mc into v_jzglbmc from DM_HB_JZGLB where dm=:new.JZGLBM;
exception
when others then
v_jzglbmc:='-9';
end;
if inserting then
insert into ) values(:new.jgh,v_bmmc,:new.xm,v_xb,:new.csrq,:new.jg,v_mz,v_whcdmc,v_jzglbmc,'人事');
insert into ) values(:new.dwh,:new.jgh,bmryxx_ryid.nextval@dblink_dxctozfoa,maxxh);
insert into ) values(kyyhjbxxbid,:new.xm,:new.xbm,:new.dwh,:new.zw);
insert into ) values(kyyhbid,:new.jgh,'u',kyyhjbxxbid,'YHJBXXB','1');
elsif deleting then
delete from where zgh=:old.jgh;
delete from where yhm=:old.jgh;
delete from where xm=:old.xm;
delete from where yhm=:old.jgh;
elsif updating then
update set zgh=:new.jgh,bm=v_bmmc,xm=:new.xm,xb=v_xb,csrq=:new.csrq,jg=:new.jg,mz=v_mz,xl=v_whcdmc,lbmc=v_jzglbmc where zgh=:old.jgh;
update set xydm=:new.dwh,yhm=:new.jgh,yhsx=maxxh where yhm=:old.jgh;
update set xm=:new.xm where xm=:old.xm;
update set yhm=:new.jgh where yhm=:old.jgh;
end if;
end if;
end;
/
create or replace trigger trig_jzgjcsjzl_portalyhb
after insert or delete or update of jgh,xm
on jg_jzgjcsjzl for each row
BEGIN
if inserting then
insert into ) values(:new.jgh,'u',:new.xm,'2');
elsif deleting then
delete from where yhm=:old.jgh;
elsif updating then
update set yhm=:new.jgh,xm=:new.xm where yhm=:old.jgh;
end if;
END;
/
create or replace trigger trig_jzgjcsjzl_zfoayhb
after insert or delete or update of jgh,xm
on jg_jzgjcsjzl for each row
BEGIN
if inserting then
insert into ) values(:new.jgh,'u','21',:new.xm,'2');
elsif deleting then
delete from where yhm=:old.jgh;
elsif updating then
update set yhm=:new.jgh,xm=:new.xm where yhm=:old.jgh;
end if;
END;
/
create or replace trigger trig_zyjszw_jsxxb after insert or delete or update of przwm on jg_zyjszwzl for each row
declare
v_przwmc varchar2(100);
BEGIN
begin
select zwxlmc into v_przwmc from dm_gb_zyjszwdm where dm=:new.przwm;
exception
when others then
v_przwmc:='-9';
end;
update set zw=v_przwmc where zgh=:new.jgh;
END;
/
create or replace trigger trig_zzmm_jsxxb
after insert or delete or update
of zzmmm on jg_zzmmsjl for each row
declare
v_zzmmmc varchar2(100);
BEGIN
begin
select mc into v_zzmmmc from dm_gb_zzmmdm where dm=:new.zzmmm;
exception
when others then
v_zzmmmc:='-9';
end;
update set zzmm=v_zzmmmc where zgh=:new.jgh;
END;
/
----------------------------------------------学生
create or replace trigger tger_xs_xsjbsjzl_ist
before insert on zfdxc.xs_xsjbsjzl
for each row
begin
----学工系统学生基本信息
insert into
(xh,bmdm,bjdm,zydm,xm,xmpy,cym,pyfs,xz,rxny,nj,sfzh,xbm,xjztm,xxnx,zyfx,ksh,bz,mm) values(:new.xh,
(case when :new.xymc in(select dwmc from XX_YXSDWJBSJZl) then (select dwh from XX_YXSDWJBSJZl where
:new.xymc=dwmc ) else 'NU' end),(case when :new.bjmc in(select bj from xx_bjsjl) then (select bh from
xx_bjsjl where :new.bjmc=bj ) else 'NULL' end) ,(case when :new.zydm is null then 'NULL' else
:new.zydm end),:new.xm,:new.xmpy,:new.cym,:new.pyfs,:new.xz,:new.rxrq,:new.nj,:new.sfzjh,(case
:new.xb when '男' then 1 when '女' then 2 else 0
end),:new.xjzt,:new.xxnx,:new.zyfx,:new.ksh,:new.bz,:new.mm);
----学工系统学生其他信息
insert into (xh,mzdm,hkszd,byzx,lydq,csrq) values
(:new.xh,:new.mzm,:new.jg,:new.byzx,:new.lydq,:new.csrq);
----学工系统学生密码表
insert into (xh,mm) values(:new.xh,:new.mm);
end;
/
create or replace trigger tger_xs_xsjbsjzl_udt
before update on zfdxc.xs_xsjbsjzl
for each row
begin
----学工系统学生基本信息
update set xh=:new.xh,bmdm=(case when :new.xymc in(select dwmc from
XX_YXSDWJBSJZl) then (select dwh from XX_YXSDWJBSJZl where :new.xymc=dwmc ) else 'NU' end),bjdm=(case
when :new.bjmc in(select bj from xx_bjsjl) then (select bh from xx_bjsjl where :new.bjmc=bj ) else
'NULL' end) ,zydm=(case when :new.zydm is null then 'NULL' else :new.zydm end),xm=(case when :new.xm
is null then 'NULL' else :new.xm
end),xmpy=:new.xmpy,cym=:new.cym,pyfs=:new.pyfs,xz=:new.xz,rxny=:new.rxrq,nj=:new.nj,sfzh=:new.sfzjh,
xbm=(case :new.xb when '男' then 1 when '女' then 2 else 0
end),xjztm=:new.xjzt,xxnx=:new.xxnx,zyfx=:new.zyfx,ksh=:new.ksh,bz=:new.bz,mm=:new.mm where
xh=:old.xh;
----学工系统学生其他信息
update set
xh=:new.xh,mzdm=:new.mzm,hkszd=:new.jg,byzx=:new.byzx,lydq=:new.lydq,csrq=:new.csrq where xh=:old.xh;
end;
/
create or replace trigger tger_xs_xsjbsjzl_del
before delete on zfdxc.xs_xsjbsjzl
for each row
begin
delete where xh=:old.xh;
delete where xh=:old.xh;
delete where xh=:old.xh;
end;
/
------------------------------------------------------------------------------------------------------------
人事
create or replace trigger trig_xydmbtozfdxc after INSERT OR DELETE OR UPDATE
of code,info ON dm_def_org FOR EACH ROW
BEGIN
IF INSERTING THEN
insert into (dwh,dwmc) values (:new.code,:new.info);
ELSIF DELETING THEN
delete from where dwh=:old.code;
ELSIF UPDATING THEN
update set dwh=:new.code,dwmc=:new.info where dwh=:old.code;
END IF;
END;
/
CREATE OR REPLACE TRIGGER trig_overall AFTER INSERT OR DELETE OR UPDATE
--of X__STAFFID,X__NAME,X__NAMESPELL,X__OLDNAME,X__BIRTHDAY,X__SEX,X__NATIONALITY,X__NATION,X__NATIVEPLACE,X__BORNPLACE,X__IDCARD,X__WORKTIME,X__HEALTHSTATE,X__BLOODTYPE,X__COLONY,X__MARRIAGESTATE,X__ORIGIN,X__PERSONSTATION,X__FILENO,X__JOINCOLLEGETIME,X__ORG,X__EDUCATIONLEVEL,X__AUTHSORT,X__STAFFSORT
ON overall FOR EACH ROW
BEGIN
IF INSERTING THEN
insert into (JGH,XM,XMPY,CYM,CSRQ,XBM,GJM,MZM,JG,CSDM,SFZJH,CJGZNY,JKZKM,XXM,GATQWM,HYZKM,JTCSM,BRCFM,DABH,LXRQ,DWH,WHCDM,BZLBM,JZGLBM,MM,xjxdm,zgxl,zgxw,rdsj,rzwsj) values (:new.X__STAFFID,:new.X__NAME,:new.X__NAMESPELL,:new.X__OLDNAME,:new.X__BIRTHDAY,:new.X__SEX,:new.X__NATIONALITY,:new.X__NATION,:new.X__NATIVEPLACE,:new.X__BORNPLACE,:new.X__IDCARD,:new.X__WORKTIME,:new.X__HEALTHSTATE,:new.X__BLOODTYPE,:new.X__COLONY,:new.X__MARRIAGESTATE,:new.X__ORIGIN,:new.X__PERSONSTATION,:new.X__FILENO,:new.X__JOINCOLLEGETIME,:new.X__ORG,:new.X__EDUCATIONLEVEL,:new.X__AUTHSORT,:new.X__STAFFSORT,'u',:new.X__FILENO,:new.X__EDUCATIONLEVEL,:new.X__DEGREE,:new.X__JOINDATE,:new.X__APPOINTDATE);
insert into (JGH,RZZGMCM,PRZWM) values (:new.X__STAFFID,:new.X__MAJORQUALIFICATION,:new.X__APPOINTDUTY);
insert into (JGH,ZZMMM,CJRQ) values (:new.X__STAFFID,:new.X__POLITICS,:new.X__JOINDATE);
ELSIF DELETING THEN
delete from where jgh=:old.X__STAFFID;
delete from where jgh=:old.X__STAFFID;
delete from where jgh=:old.X__STAFFID;
ELSIF UPDATING THEN
update set JGH=:new.X__STAFFID,XM=:new.X__NAME,XMPY=:new.X__NAMESPELL,CYM=:new.X__OLDNAME,CSRQ=:new.X__BIRTHDAY,XBM=:new.X__SEX,GJM=:new.X__NATIONALITY,MZM=:new.X__NATION,JG=:new.X__NATIVEPLACE,CSDM=:new.X__BORNPLACE,SFZJH=:new.X__IDCARD,CJGZNY=:new.X__WORKTIME,JKZKM=:new.X__HEALTHSTATE,XXM=:new.X__BLOODTYPE,GATQWM=:new.X__COLONY,HYZKM=:new.X__MARRIAGESTATE,JTCSM=:new.X__ORIGIN,BRCFM=:new.X__PERSONSTATION,DABH=:new.X__FILENO,LXRQ=:new.X__JOINCOLLEGETIME,DWH=:new.X__ORG,WHCDM=:new.X__EDUCATIONLEVEL,BZLBM=:new.X__AUTHSORT,JZGLBM=:new.X__STAFFSORT,XJXDM=:new.X__FILENO,ZGXL=:new.X__EDUCATIONLEVEL,ZGXW=:new.X__DEGREE,RDSJ=:new.X__JOINDATE,RZWSJ=:new.X__APPOINTDATE where jgh=:old.X__STAFFID;
update set JGH=:new.X__STAFFID,RZZGMCM=:new.X__MAJORQUALIFICATION,PRZWM=:new.X__APPOINTDUTY where jgh=:old.X__STAFFID;
update set JGH=:new.X__STAFFID,ZZMMM=:new.X__POLITICS,CJRQ=:new.X__JOINDATE where jgh=:old.X__STAFFID;
END IF;
END;
/
--------------------------------------------------------------------------------------------------------------------
教务
---校区
create or replace trigger tger_xqdm_ist
before insert on zfxfzb.xqdmb
for each row
begin
insert into (xqh,xqm) values(:new.xqdm,:new.xqmc);
end;
/
create or replace trigger tger_xqdm_udt
before update on zfxfzb.xqdmb
for each row
begin
update set xqh=:new.xqdm,xqm=:new.xqmc where xqh=:old.xqdm;
end;
/
create or replace trigger tger_xqdm_del
before delete on zfxfzb.xqdmb
for each row
begin
delete where xqh=:old.xqdm;
end;
---专业
create or replace trigger tger_zydm_ist
before insert on zfxfzb.zydmb
for each row
begin
insert into (zyh,zymc,zyjc,zyywmc,dwh,xz,bzkzym) values(:new.zydm,:new.zymc,:new.zyjc,:new.zyywmc,:new.ssxydm,:new.xz,:new.tjzydm);
end;
/
create or replace trigger tger_zydm_udt
before update on zfxfzb.zydmb
for each row
begin
update set zyh=:new.zydm,zymc=:new.zymc,zyjc=:new.zyjc,zyywmc=:new.zyywmc,dwh=:new.ssxydm,xz=:new.xz,bzkzym=:new.ssxydm where zyh=:old.zydm;
end;
/
create or replace trigger tger_zydm_del
before delete on zfxfzb.zydmb
for each row
begin
delete where zyh=:old.zydm;
end;
/
---班级
create or replace trigger tger_bjdm_ist
before insert on zfxfzb.bjdmb
for each row
begin
insert into (bh,bj,bzrjgh,fdyh,zyh,bjjc,zyfx,ssxydm,nj,ssxqdm,xz,cc) values(:new.bjdm,:new.bjmc,:new.bzrzgh,:new.fdyxm,:new.sszydm,:new.bjjc,:new.zyfx,:new.ssxydm,:new.nj,:new.ssxqdm,:new.xz,:new.cc);
end;
/
create or replace trigger tger_bjdm_udt
before update on zfxfzb.bjdmb
for each row
begin
update set bh=:new.bjdm,bj=:new.bjmc,bzrjgh=:new.bzrzgh,fdyh=:new.fdyxm,zyh=:new.sszydm,bjjc=:new.bjjc,zyfx=:new.zyfx,ssxydm=:new.ssxydm,nj=:new.nj,ssxqdm=:new.ssxqdm,xz=:new.xz,cc=:new.cc where bh=:old.bjdm;
end;
/
create or replace trigger tger_bjdm_del
before delete on zfxfzb.bjdmb
for each row
begin
delete where bh=:old.bjdm;
end;
/
--学生
create or replace trigger tger_xsjbxx_ist
before insert on zfxfzb.xsjbxxb
for each row
declare
v_bjdm varchar2(50);
v_xydm varchar2(50);
begin
begin
select xydm into v_xydm from xydmb where xymc=:new.xy;
exception
when others then
v_xydm:='9';
end;
begin
select bjdm into v_bjdm from bjdmb where bjmc=:new.xzb;
exception
when others then
v_bjdm:='9';
end;
----数据中心学生基本数据子类
insert into (xh,xm,xmpy,cym,csrq,jg,sfzjh,xymc,zydm,zymc,bjmc,mz,pyfs,ksh,xxnx,xz,xb,zyfx,pyfx,xjzt,sfzx,sfzc,bz,nj,rxrq,LYDQ,BYZX,SSH,DZYXDZ,LXDH,ZKZH,JTSZD,SFLXS,TELNUMBER,TELLX,CC,YZBM,RXZF,YYCJ,zzmm,mm) values(:new.xh,:new.xm,:new.xmpy,:new.zym,:new.csrq,:new.jg,:new.sfzh,:new.xy,:new.zydm,:new.zymc,:new.xzb,:new.mz,:new.xxxs,:new.ksh,:new.xxnx,:new.xz,:new.xb,:new.zyfx,:new.pyfx,:new.xjzt,:new.sfzx,:new.sfzc,:new.bz,:new.dqszj,:new.rxrq,:new.LYDQ,:new.BYZX,:new.SSH,:new.DZYXDZ,:new.LXDH,:new.ZKZH,:new.JTSZD,:new.SFLXS,:new.TELNUMBER,:new.TELLX,:new.CC,:new.YZBM,:new.RXZF,:new.YYCJ,:new.zzmm,:new.mm);
----数据中心学籍基本数据子类
insert into (xh,yxsh,zym,bh) values(:new.xh,v_xydm,:new.zydm,v_bjdm);
end;
/
create or replace trigger tger_xsjbxx_udt
before update on zfxfzb.xsjbxxb
for each row
declare
v_bjdm varchar2(50);
v_xydm varchar2(50);
begin
----数据中心学生基本数据子类
begin
select xydm into v_xydm from xydmb where xymc=:new.xy;
exception
when others then
v_xydm:='9';
end;
begin
select bjdm into v_bjdm from bjdmb where bjmc=:new.xzb;
exception
when others then
v_bjdm:='9';
end;
update set xh=:new.xh,xm=:new.xm,xmpy=:new.xmpy,cym=:new.zym,csrq=:new.csrq,jg=:new.jg,sfzjh=:new.sfzh,xymc=:new.xy,zydm=:new.zydm,zymc=:new.zymc,bjmc=:new.xzb,mz=:new.mz,pyfs=:new.xxxs,ksh=:new.ksh,xxnx=:new.xxnx,xz=:new.xz,xb=:new.xb,zyfx=:new.zyfx,pyfx=:new.pyfx,xjzt=:new.xjzt,sfzx=:new.sfzx,sfzc=:new.sfzc,bz=:new.bz,nj=:new.dqszj,rxrq=:new.rxrq,lydq=:new.LYDQ,byzx=:new.BYZX,ssh=:new.SSH,dzyxdz=:new.DZYXDZ,lxdh=:new.LXDH,zkzh=:new.ZKZH,jtszd=:new.JTSZD,sflxs=:new.SFLXS,TELNUMBER=:new.TELNUMBER,TELLX=:new.TELLX,cc=:new.CC,YZBM=:new.YZBM,RXZF=:new.RXZF,YYCJ=:new.YYCJ,zzmm=:new.zzmm,mm=:new.mm where xh=:old.xh;
----数据中心学籍基本数据子类
update set xh=:new.xh,yxsh=v_xydm,zym=:new.zydm,bh=v_bjdm where xh=:old.xh;
end;
/
create or replace trigger tger_xsjbxx_del
before delete on zfxfzb.xsjbxxb
for each row
begin
delete where xh=:old.xh;
delete where xh=:old.xh;
end;
/
--外聘教师
create or replace trigger trig_wpjs_zfdxc after insert or delete or update of zgh,xm,bm on jsxxb for each row
declare
v_bmdm varchar2(10);
len number;
begin
select count(jgh) into len from where jgh=:old.zgh and sjly='人事';
if len=0 and :new.sjly||'A'<>'人事A' then--不存在人事的数据
begin
select xydm into v_bmdm from xydmb where xymc=:new.bm;
exception
when others then
v_bmdm:='-9';
end;
if inserting then
insert into ) values(:new.zgh,:new.xm,v_bmdm,'教务');
elsif deleting then
delete from where jgh=:old.zgh;
elsif updating then
update set jgh=:new.zgh,xm=:new.xm,dwh=v_bmdm where jgh=:old.zgh;
end if;
end if;
end;
/
**********本博客所有内容均为原创,如有转载请注明作者和出处!!!**********
QQ: 252803295
Email:dbathink@hotmail.com
尖峰官网:
尖峰淘宝:
WEIBO:
尖峰OCP认证考试群297227448
尖峰OCM认证考试群99606943
尖峰MySQL研究院群314746420
尖峰JAVA研究院群
315405063
尖峰Hadoop研究院群366294602
尖峰线上技术分享群252296815
尖峰SQL优化研究院群250057366