删除前日志触发错误,我找不到错误

时间:2018-10-30 14:37:59

标签: oracle plsql

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

我尝试过,将旧到旧引用为新。

也许错误在于变量。

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

1 个答案:

答案 0 :(得分:3)

这里的错误似乎很清楚-您有一个语句级触发器,而不是行级触发器(没有for each row子句)和you can't use the old/new pseudorecords at statement level

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

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调用。