trigger练习

create or replace trigger WS_档号生成

before insert or update on S3_MAIN_WSDA

for each row

declare

--自治事务应用(子事务的commit,rollback不影响父事务的状态)

pragma autonomous_transaction;

maxstjjh s3_main_wsda .s3_jjh%type;

you number;

zbm number;

fl s3_wsfl.s3_fl%type;

coun number;

a number;

gdbm nvarchar2(50);

bmid nvarchar2(50);

qzdw nvarchar2(100);


begin

:new.s3_tm :=translate(:new.s3_tm,chr(13)||chr(10),' ');

:new.s3_gdbm:=replace(:new.s3_gdbm,'(','(');

:new.s3_gdbm:=replace(:new.s3_gdbm,')',')');

if :new.s3_wj_cwrq is not null and :new.s3_nd is null and :new.s3_sjly<>'历史资料' and :new.s3_sjly<>'历史档案' then

:new.s3_nd:=to_char(:new.wj_cwrq,'YYYY');

end if;

:new.s3_fbfw:=replace(:new.s3_fbfw,'、',',');

select count(id) into coun from s3_wsfl where s3_jgid=:new.s3_jgid;

if coun>0 then 

select s3_fl into fl from s3_wsfl where s3_jgid=:new.s3_jgid;

else

fl:='三级分类';

end if;

:new.s3_fl:=fl;

case 

when updating then

--生成档号

if :new.s3_bgqx is not null and :new.s3_tjsh='档案管理员通过审核' and :new.s3_gdyjbs='已归档' and (:new.s3_isdelete =0 or :new.s3_isdelete is null) and :new.s3_sjly<>'历史资料' and :new.s3_sjly<>'资料' and :new.s3_sjly<>'部门资料' and :new.s3_jyzt='在库' then

if(:new.s3_fl='二级分类') then 

if :new.s3_jjh is null then

select nvl(max(to_number(S3_JJH))+1,1) into maxstjjh from S3_MAIN_WSDA where s3_jgid=:new.s3_jgid and s3_nd=:new.s3_nd and s3_bgqx=:new.s3_bgqx and (s3_isdelete=0 or s3_isdelete is null);

:new.s3_jjh:=maxstjjh;

end if;

if :new.s3_dh is null or (:new.s3_dh is not null and :new.s3_jjh<>:old.s3_jjh) then 

:new.s3_dh:=:new.qzdw||'-'||:new.s3_nd||'-'||:new.s3_bgqx||'-'||lpad(:new.s3_jjh,3,'0');

:new.s3_dha:=:new.s3_nd||'-'||:new.s3_bgqx||'-'||lpad(:new.s3_jjh,3,'0');

end if;

end if;

if(:new.s3_fl='三级分类') then

if :new.s3_jjh is null then

select nvl(max(to_number(S3_JJH))+1,1) into maxstjjh from S3_MAIN_WSDA where s3_jgid=:new.s3_jgid and s3_nd=:new.s3_nd and s3_gdbm=:new.s3_gdbm and s3_bgqx=:new.s3_bgqx and (s3_isdelete=0 or s3_isdelete is null);

:new.s3_jjh:=maxstjjh;

end if;

if :new.s3_dh is null or (:new.s3_dh is not null and :new.s3_jjh<>:old.s3_jjh) then

:new.s3_dh:=:new.s3_qzdw||'-'||:new.s3_nd||'-'||:new.s3_gdbm||'-'||:new.s3_bgqx||'-'||lpad(:new.s3_jjh,3,'0');

:new.s3_dha:=:new.s3_nd||'-'||:new.s3_gdbm||'-'||:new.s3_bgqx||'-'||lpad(:new.s3_jjh,3,'0');

end if;

end if;

end if;



你可能感兴趣的:(oracle,trigger)