0

错误在哪里,在我看来很好,但我得到了错误。

我试过了,这个引用旧的和新的一样新。

也许错误出在变量中。

SQL> create or replace trigger before_delete
      2         before delete
      3     on accounts
      4  declare 
      5     v_username varchar2(20);
      6   begin
      7     select user into v_username
      8     from dual;
      9     insert into accounts_history
     10     (
     11         id, 
     12             new_name,
     13             old_name,
     14             new_amount,
     15             old_amount,
     16             change_date,
     17         delted_by
     18     )
     19     values
     20     (      
     21         :old.acc_id
     22         :old.acc_name
     23         :old.acc_name
     24         :old.acc_amount
     25         :old.acc_amount
     26         sysdate,
     27         v_username
     28   );
     29   end;
     30   /
        insert into accounts_history
            *
    ERROR at line 9:
    ORA-04082: NEW or OLD references not allowed in table level triggers
4

1 回答 1

3

这里的错误似乎相当清楚 - 您有一个语句级触发器,而不是一个行级触发器(无for each row子句),并且您不能在语句级使用旧/新伪记录。

看起来这实际上是您想要的,您需要添加该子句:

create or replace trigger before_delete
before delete
on accounts
for each row
begin
    insert into accounts_history
    (
        id,
        new_name,
        old_name,
        new_amount,
        old_amount,
        change_date,
        delted_by
    )
    values
    (
        :old.acc_id,
        :old.acc_name,
        :old.acc_name,
        :old.acc_amount,
        :old.acc_amount,
        sysdate,
        user
   );
end;
/

您不需要从对偶查询来获取user变量,您可以直接分配它;但你根本不需要那个变量;user您可以直接在values子句中引用调用。

于 2018-10-30T14:42:24.930 回答