0

我有两张桌子mainhist. main每当其日期时间列被修改为hist并将修改后的列重置mainnull除一列时,我想从表中复制一行。但是我得到了变异触发错误。请帮忙。下面是两个触发器,

CREATE OR REPLACE TRIGGER INS_HIS
AFTER UPDATE OF datetime ON main
FOR EACH ROW 
  INSERT INTO HIST 
    VALUES (Col2 = :new.col2, Col3= :new.Col3)
END;


CREATE OR REPLACE TRIGGER UPD_NUL_MAIN
AFTER INSERT ON HIST
FOR EACH ROW
   UPDATE Main 
      SET (Col2 = NULL
           Col3= NULL)
    WHERE HIST.datetime = main.datetime;
END
4

2 回答 2

2

假设您只想更改 main 中触发更新的行,如果您将第一个触发器更改为 BEFORE UPDATE 一个并在那里设置 col2 和 col3,则可以完全摆脱第二个触发器:

CREATE OR REPLACE TRIGGER INS_HIS
BEFORE UPDATE OF datetime ON main
FOR EACH ROW          
BEGIN
  INSERT INTO HIST
    (col2,
     col3)
  VALUES
    (:new.col2,
     :new.Col3);
  :new.col2 := NULL;
  :new.col3 := NULL;
END;

顺便说一句:您的触发器语法错误 - 您不能使用 (col2 = :new.col2)。

于 2013-12-17T12:37:21.287 回答
1

为了做到这一点,我认为您只需要一个触发器?

CREATE OR REPLACE TRIGGER ins_his BEFORE UPDATE OF datetime ON main
FOR EACH ROW 
BEGIN

  INSERT INTO hist ( col2     , col3      )
            VALUES ( :new.col2, :new.col3 );

  :new.col2 := NULL;
  :new.col3 := NULL;

END;
/

示例输出:

SQL*Plus: Release 11.2.0.1.0 Production on Tue Dec 17 13:17:08 2013

Copyright (c) 1982, 2010, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, Real Application Clusters, Automatic Storage Management,
OLAP,
Data Mining and Real Application Testing options

SQL> create table main ( datetime DATE, col2 NUMBER, col3 NUMBER );

Table created.

SQL> create table hist ( col2 NUMBER, col3 NUMBER );

Table created.

SQL> CREATE OR REPLACE TRIGGER ins_his BEFORE UPDATE OF datetime ON main
  2  FOR EACH ROW
  3  BEGIN
  4    --
  5    INSERT INTO hist ( col2     , col3      )
  6              VALUES ( :new.col2, :new.col3 );
  7    --
  8    :new.col2 := NULL;
  9    :new.col3 := NULL;
 10    --
 11  END;
 12  /

Trigger created.

SQL> insert into main( datetime, col2, col3 )
  2  values ( sysdate, 5, 10 );

1 row created.

SQL> select * from main;

DATETIME        COL2       COL3
--------- ---------- ----------
17-DEC-13          5         10

SQL> select * from hist;

no rows selected

SQL> update main set datetime = sysdate-1;

1 row updated.

SQL> select * from main;

DATETIME        COL2       COL3
--------- ---------- ----------
16-DEC-13

SQL> select * from hist;

      COL2       COL3
---------- ----------
         5         10

SQL>
于 2013-12-17T12:40:53.667 回答