我有两张表main
和hist
。我想在{date}列修改为main
时将hist
表格中的一行复制,并将main
中的修改后的列重置为null
除了一列。但是我正在改变触发错误。请帮忙。
以下是两个触发器,
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
答案 0 :(得分: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;
BTW:您的触发器语法错误 - 您无法使用(col2 =:new.col2)。
答案 1 :(得分: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>