Oracle觸發器寫法和舉例

1、declare 聲明變量後的每個變量都要加分號;
2、所有的語句結束和sql語句結尾,都要加分號;
3、變量賦值 variable :='1234'
   如:recordId := '1234';
4、插入列數據獲取 :new.colname 
   如:new.exec_record_id
5、sql查詢寫入變量 select colname into variable
   如: select verify_date into execDate   from orders_executed_record where record_id='1234';

 

觸發器舉例:

CREATE OR REPLACE TRIGGER "TR_NUTRITION"
 after update on inp_bill_detail
  for each row
declare
    myexp exception;
    execDate date;
    recordId varchar2(30);
begin
    recordId :=:new.exec_record_id;
    if :new.exec_record_id is not null then
        select nvl(oen.verify_date,oer.execute_date_time) into execDate
        from orders_executed_record oer left join orders_executed_nutrition oen on oer.record_id=oen.record_id
        where oer.record_id = recordId ;
        if execDate is not null then
           raise myexp;
         end if;
    end if;
    exception
        when myexp then raise_application_error('-20002', '計費後,執行(覈對)日期不能爲空,計費ID=' || :new.detail_no || ' 執行ID=' || recordId);
end ;



   

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章