oracle触发器与:new,:old的使用

:new --为一个援用最新的列值;
:old --为一个援用以前的列值; 那两个变量只有在利用了要害字 "FOR EACH ROW"时才存在.且update语句两个皆有,而insert只有:new ,delect 只有:old;

系统中的触发器实例:

 

create or replace trigger JBPM.TIB_DEPLOYBYMOVEPAPER before --before暗示在操做完成前触发,after暗示在完成后触发
insert --发生发水插进数据操做触发
on JBPM.DEPLOYBYMOVEPAPER for each row --指定触发器每止触发一次
declare
    integrity_error exception; --用户自界说破绽
    errno            integer;
    errmsg           char(200);
    dummy            integer;
    found            boolean;
    row_count number;
    max_num deploybymove***.num%type; --遵照表的字段界说变量类型
    zero_today deploybymove***.num%type;
begin
-- Column "ID" uses sequence SEQUENCE_38
case
when inserting then --SQL语句只能利用:new不凡变量
select SEQUENCE_38.NEXTVAL INTO :new.ID from dual;--主键自删
zero_today :="0391"||to_char(sysdate,"yymmdd")||lpad(to_char(0),4,"0");--lpad函数界说字符串少度,为4不敷时用0补全
    select count(*) into row_count from deploybymove***;
    if row_count = 0 then
        max_num:=zero_today;--:=给变量赋值
    else
      select max(num) into max_num from deploybymove***;
    end if;
    if max_num
        max_num:=zero_today;
    end if;
    if :new.num is null then
    :new.num:=lpad(to_char(to_number(max_num)+1),14,"0"); --递删编号,有14为数字构成
    end if;
when updating("num") then--:new、:old皆可以用
if :new.num!=:old.num then
      :new.num:=:old.num;
end if;
end case;
-- Errors handling
exception
    when integrity_error then
       raise_application_error(errno, errmsg); --扔出异常语句
end;
/

转自:http://blog.itpub.net/21459650/viewspace-1116895

猜你喜欢

转载自jokze.iteye.com/blog/2064202