2015-09-04 53 views
0
create or replace trigger t1 
    before update of price on book 
declare 
    vdif number; 
begin 
    vdif:=:new.price-:old.price; 
    dbms_output.put_line('Price Diff is '||vdif); 
end; 

我得到這個錯誤:由於錯誤消息指出觸發器的SQL甲骨文

NEW or OLD references not allowed in table level triggers

回答

5

,你不能在表級觸發器使用:new:old。你需要一個行級觸發器。所以,你需要添加FOR EACH ROW到你的宣言

create or replace trigger t1 
    before update of price on book 
    for each row 
declare 
    vdif number; 
begin 
    vdif:=:new.price-:old.price; 
    dbms_output.put_line('Price Diff is '||vdif); 
end; 

當然,在現實中,你永遠不會寫一個觸發器,它只是寫給dbms_output緩衝區(也不需要編寫生產代碼依賴於任何人看到任何寫入dbms_output的內容)。但我認爲你是一名學生,你只是做這個作業的一部分。

+0

是的,我現在得到它的工作正常 – Parvez

0

您寫的觸發器是表級別觸發器,並且表級別觸發器會針對表上的每個操作觸發一次。因此,例如,如果您有更新多行的查詢,則觸發器只會被調用一次:new和:old不知道要影響的行。

你真正想要的是在觸發器定義(「更新前的...」下)添加FOR EACH ROW條款,這將使你的觸發一個行級觸發器,它會火要更新的每一行。