[轉]oracle觸發器與:new,:old的使用

來源:互聯網
上載者:User

標籤:

: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 deploybymovepaper.num%type; //根據表的欄位定義變數類型
    zero_today deploybymovepaper.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 deploybymovepaper;
    if row_count = 0 then
        max_num:=zero_today;//:=給變數賦值
    else
      select max(num) into max_num from deploybymovepaper;
    end if;
    if max_num<zero_today then
        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;
/

[轉]oracle觸發器與:new,:old的使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.