Oracle在更新前触发了一个触发器

时间:2016-02-18 08:58:20

标签: oracle triggers

我有表CREATE or REPLACE TRIGGER SET_OLDVALUE BEFORE update ON product FOR EACH ROW begin :new.OLD_VALUE = :old.VALUE; end;

在更新产品的价值之前,我想触发一个触发器以保存旧值。为此,我创建了以下触发器:

update product set value='newVal' where product_name='prodName';

但是当我尝试更新产品表时:

 [Error Code: 4098, SQL State: 42000]  ORA-04098: trigger 'product.SET_OLDVALUE' is invalid and failed re-validation

我收到此错误:

<a>

1 个答案:

答案 0 :(得分:1)

我认为它是无效的,因为你错过了冒号(:)

试试这个

         :new.OLD_VALUE := :old.VALUE;

create table product (
id      integer,
value   varchar2(10),
old_value varchar2(10)
);

CREATE or REPLACE TRIGGER SET_OLDVALUE BEFORE update ON product
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
    begin
         :new.OLD_VALUE := :old.VALUE;
    end;

insert into product (id, value) values (1, 'SHRIRAM');

commit;

update product
set value = 'NEW NAME'
where id = 1;

commit;

select *
  from product;

1  | NEW NAME | SHRIRAM