2015-09-04 48 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条款,这将使你的触发一个行级触发器,它会火要更新的每一行。