我有复合触发器的问题。:new.value 在指定后部分时,在每行部分之前为空白/空。
创建表部分:
DROP TABLE test_tab;
CREATE TABLE test_tab
( ID_TEST_TAB NUMBER
);
INSERT INTO test_tab VALUES
(1
);
INSERT INTO test_tab VALUES
(2
);
触发器的部分:
CREATE OR REPLACE TRIGGER TEST_COMP_TRIGGER
FOR UPDATE ON test_tab
COMPOUND TRIGGER
---
BEFORE EACH ROW
IS
BEGIN
dbms_output.put_line('TEST_COMP_TRIGGER: BEFORE EACH ROW');
dbms_output.put_line('>>>> old: ' || :old.ID_TEST_TAB );
dbms_output.put_line('>>>> new: ' || :new.ID_TEST_TAB );
NULL;
END BEFORE EACH ROW;
---
AFTER STATEMENT
IS
BEGIN
NULL;
END AFTER STATEMENT;
END TEST_COMP_TRIGGER;
/
后:
UPDATE test_tab SET test_tab.ID_TEST_TAB=test_tab.ID_TEST_TAB;
我得到:
TEST_COMP_TRIGGER: BEFORE EACH ROW
>>>> old: 1
>>>> new:
TEST_COMP_TRIGGER: BEFORE EACH ROW
>>>> old: 2
>>>> new:
如果省略after部分,则结果正确:
CREATE OR REPLACE TRIGGER TEST_COMP_TRIGGER
FOR UPDATE ON test_tab
COMPOUND TRIGGER
---
BEFORE EACH ROW
IS
BEGIN
dbms_output.put_line('TEST_COMP_TRIGGER: BEFORE EACH ROW');
dbms_output.put_line('>>>> old: ' || :old.ID_TEST_TAB );
dbms_output.put_line('>>>> new: ' || :new.ID_TEST_TAB );
NULL;
END BEFORE EACH ROW;
END TEST_COMP_TRIGGER;
/
后:
UPDATE test_tab SET test_tab.ID_TEST_TAB=test_tab.ID_TEST_TAB;
我得到:
TEST_COMP_TRIGGER: BEFORE EACH ROW
>>>> old: 1
>>>> new: 2
TEST_COMP_TRIGGER: BEFORE EACH ROW
>>>> old: 2
>>>> new: 2
为什么在第一种情况下 :new.value 是空白的?
------------ 更新我按照你的指示:代码:
create or replace trigger TEST_COMP_TRIGGER
FOR UPDATE ON test_tab
COMPOUND TRIGGER
---- BEFORE
BEFORE STATEMENT
IS
BEGIN
dbms_output.put_line('TEST_COMP_TRIGGER: BEFORE STATEMENT');
NULL;
END BEFORE STATEMENT;
---- AFTER
AFTER STATEMENT IS
BEGIN
dbms_output.put_line('TEST_COMP_TRIGGER: AFTER STATEMENT');
NULL;
END AFTER STATEMENT;
---- BEFORE EACH
BEFORE EACH ROW IS
BEGIN
dbms_output.put_line('TEST_COMP_TRIGGER: BEFORE EACH ROW');
dbms_output.put_line('>>>> old: ' || :old.ID_TEST_TAB );
dbms_output.put_line('>>>> new: ' || :new.ID_TEST_TAB );
NULL;
END BEFORE EACH ROW;
END TEST_COMP_TRIGGER;
/
-----
SET serveroutput ON format wraped;
UPDATE test_tab SET test_tab.ID_TEST_TAB=test_tab.ID_TEST_TAB;
/
给出预期结果:
2 rows updated.
TEST_COMP_TRIGGER: BEFORE STATEMENT
TEST_COMP_TRIGGER: BEFORE EACH ROW
>>>> old: 1
>>>> new: 1
TEST_COMP_TRIGGER: BEFORE EACH ROW
>>>> old: 2
>>>> new: 2
TEST_COMP_TRIGGER: AFTER STATEMENT
但是当我添加 AFTER EACH ROW 语句时:
create or replace trigger TEST_COMP_TRIGGER
FOR UPDATE ON test_tab
COMPOUND TRIGGER
---- BEFORE
BEFORE STATEMENT
IS
BEGIN
dbms_output.put_line('TEST_COMP_TRIGGER: BEFORE STATEMENT');
NULL;
END BEFORE STATEMENT;
---- AFTER
AFTER STATEMENT IS
BEGIN
dbms_output.put_line('TEST_COMP_TRIGGER: AFTER STATEMENT');
NULL;
END AFTER STATEMENT;
---- BEFORE EACH
BEFORE EACH ROW IS
BEGIN
dbms_output.put_line('TEST_COMP_TRIGGER: BEFORE EACH ROW');
dbms_output.put_line('>>>> old: ' || :old.ID_TEST_TAB );
dbms_output.put_line('>>>> new: ' || :new.ID_TEST_TAB );
NULL;
END BEFORE EACH ROW;
---- AFTER EACH
AFTER EACH ROW
IS
BEGIN
dbms_output.put_line('TEST_COMP_TRIGGER: AFTER EACH ROW');
END AFTER EACH ROW;
END TEST_COMP_TRIGGER;
/
-----
SET serveroutput ON format wraped;
UPDATE test_tab SET test_tab.ID_TEST_TAB=test_tab.ID_TEST_TAB;
/
我还是很空:新:
2 rows updated.
TEST_COMP_TRIGGER: BEFORE STATEMENT
TEST_COMP_TRIGGER: BEFORE EACH ROW
>>>> old: 1
>>>> new:
TEST_COMP_TRIGGER: AFTER EACH ROW
TEST_COMP_TRIGGER: BEFORE EACH ROW
>>>> old: 2
>>>> new:
TEST_COMP_TRIGGER: AFTER EACH ROW
TEST_COMP_TRIGGER: AFTER STATEMENT