2

我正在尝试编写历史触发器,但无法使其正常工作。

这是代码:

create or replace
TRIGGER abc BEFORE
    UPDATE ON abc REFERENCING OLD AS oldValue NEW AS newValue
    FOR EACH ROW 
    BEGIN
        INSERT
    INTO history
        (
            id,
            record_id,
            col_name,
            old_val,
            new_val
        )
        VALUES
        (
            MF_SEQ_HISTORY.nextval,
            :oldvalue.id,
            :oldvalue.column_name,
            :oldvalue.object_value,
            :newvalue.object_value
        );
        END;

如您所见,我正在尝试保存已更新的列名、值和新值。

我得到的错误是:

Error(19,13): PLS-00049: bad bind variable 'OLDVALUE.COLUMN_NAME'
Error(20,13): PLS-00049: bad bind variable 'OLDVALUE.OBJECT_VALUE'
Error(21,13): PLS-00049: bad bind variable 'NEWVALUE.OBJECT_VALUE'

任何帮助将不胜感激。

谢谢。

4

3 回答 3

0

也许最好使用提供的功能来启用 Oracle 数据库中的表的版本。看一下工作区管理器:DBMS_WM.EnableVersioning。

http://www.oracle-base.com/articles/9i/workspace-management-9i.php

exec dbms_wm.enableversioning('LANDEN','view_wo_overwrite');

insert into landen values (1,'Nederland','The Netherlands','Nederland','NL','NL','NL',sysdate);
update landen set naamnl = 'Holland' where pk = 1;

select pk,naamnl from landen;

1   Holland

select pk,naamnl,retiretime from landen_lt;

1   Nederland   08-11-2012 11:01:14,466296000 +01:00
1   Holland 
于 2012-11-08T09:59:07.277 回答
0

希望这是您正在寻找的东西:- http://birijan.com.np/?q=oracle+trigger+-+column+level+auditing

于 2012-11-08T09:55:08.097 回答
0

您提供的代码没有任何问题。有用。但是表 ABC 是否有名为 column_name 和 object_value 的列?如果是这样,请给出 sqlplus 的输出DESC ABC

SQL> create table abc (id number, column_name varchar2(30), object_value varchar2(30));

Table created.

SQL> create table history (id number, record_id number, col_name varchar2(30), old_val varchar2(30), new_val varchar2(30));

Table created.

SQL> create sequence mf_seq_history start with 1;

Sequence created.

SQL> create or replace
  2  TRIGGER abc BEFORE
  3      UPDATE ON abc REFERENCING OLD AS oldValue NEW AS newValue
  4      FOR EACH ROW
  5      BEGIN
  6          INSERT
  7      INTO history
  8          (
  9              id,
 10              record_id,
 11              col_name,
 12              old_val,
 13              new_val
 14          )
 15          VALUES
 16          (
 17              MF_SEQ_HISTORY.nextval,
 18              :oldvalue.id,
 19              :oldvalue.column_name,
 20              :oldvalue.object_value,
 21              :newvalue.object_value
 22          );
 23          END;
 24  /

Trigger created.

SQL> insert into abc values (1, 'COL', 'TEST');

1 row created.

SQL> insert into abc values (1, 'COL2', 'TEST2');

1 row created.

SQL> update abc set object_value ='a';

2 rows updated.

SQL> select * from history;

        ID  RECORD_ID COL_NAME
---------- ---------- ------------------------------
OLD_VAL                        NEW_VAL
------------------------------ ------------------------------
         1          1 COL
TEST                           a

         2          1 COL2
TEST2                          a


SQL>
于 2012-11-08T09:22:19.533 回答