创建历史触发器

时间:2012-11-08 09:13:10

标签: oracle triggers history

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

这是代码:

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'

任何帮助将不胜感激。

感谢。

3 个答案:

答案 0 :(得分:0)

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

的sqlplus输出
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>

答案 1 :(得分:0)

答案 2 :(得分: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