2016-02-20 21 views
0

我正在寫一個觸發器,它將觸發更新並插入到一個表上並插入到另一個表中。觸發器看起來像這樣。觸發器沒有觸發使用引用

create or replace trigger update_test_history 
    before insert or update on demo.test_req REFERENCING NEW AS NEW OLD AS old 
    for each row 
declare 
    v_count number(1); 
begin 

    if :old.quote_request_id != null then 
     --Update History table 
     insert into demo.test_req_history 
     (history_id, 
      req_id) 
     values 
     (isisdba.req_hist_seq.nextval, 
      :old.req_id); 
     end if; 

end update_test_history; 

此觸發器不適用於DML。但是,如果我刪除if條件,那麼它開始工作。 有人可以告訴我這是什麼錯誤。 在此先感謝。

+0

定義「不工作」? 'null'永遠不會等於任何包含'null'的值。 'null'也永遠不等於另一個包括'null'的值。所以'something!= null'永遠不會計算爲'true'。也許你想要':old.quote_request_id不爲空'。 –

+0

謝謝Jastin,它正在工作。 –

回答

1

null永遠不會等於任何值,包括nullnull也永遠不等於另一個值,包括null。所以聲明

if(<<some expression>> != null) 
then 
    <<do something>> 
end if; 

將永遠進入<<do something>>塊。無論是將

if(<<some expression>> = null) 
then 
    <<do something>> 
end if; 

你必須使用三元邏輯,你正在處理null值BU說is nullis not null。看起來你可能想要

if(:old.quote_request_id is not null) 
the 
    <<do something>> 
end if;