触发器未按预期设置列值

时间:2013-06-10 10:01:37

标签: oracle triggers oracle11g

我有一个触发器,可以从表中更新名为value的列,但是当我执行时 触发器value列的值未正确设置,它始终设置为0.触发器为:

create or replace
TRIGGER trigger2 
AFTER INSERT ON TABLE_AUX
FOR EACH ROW
DECLARE
 BEGIN
  .....

    IF V_VALUE > 0 THEN       
     /* V_VALUE IS GREATER THAN 0. THIS WORKS FINE */

         SELECT COUNT(*) "NUM" INTO V_NUM
         FROM TABLE1 T1,
          WHERE ............;
         /* V_NUM ALWAYS CONTAINS THE CORRECT VALUE */

         /* THE BUG IS IN THIS UPDATE */
          UPDATE TABLE_UPDATE 
          SET "VALUE" = V_VALUE - V_NUM , FIELD_OK = 'OK' /* ONLY FAILS THE FIRST SET */
          WHERE ID= (SELECT MAX(ID)   
                  FROM TABLE_UPDATE TAB
                  WHERE .....); 

       END IF;    
       .....

END;

当我之前调用此触发器时,我有另一个触发器插入名为TABLE_AUX的临时表:

create or replace
TRIGGER trigger1
BEFORE INSERT ON TABLE_UPDATE 
FOR EACH ROW 
DECLARE
 ....
BEGIN

  INSERT INTO TABLE_AUX VALUES(....)
END;

TABLE_UPDATEtrigger2中使用的表格相同。 V_VALUE中使用的变量V_NUMtrigger1NUMBER变量,value列为NUMBER(30,20)

2 个答案:

答案 0 :(得分:0)

更改自:

create or replace
TRIGGER trigger2 
AFTER INSERT ON TABLE_AUX
FOR EACH ROW

为:

create or replace
TRIGGER trigger2 
AFTER INSERT ON TABLE_UPDATE
FOR EACH ROW

...然后修复其余的错误。

答案 1 :(得分:0)

我重新创建了你的例子。哪里必须做出改变?

CREATE TABLE table_update
(
    id       NUMBER
,   "VALUE"  NUMBER
,   FIELD_OK VARCHAR2(10)
);

CREATE TABLE table_aux
(
    "VALUE"  NUMBER
);

CREATE OR REPLACE
TRIGGER trigger1
BEFORE INSERT ON table_update
FOR EACH ROW
DECLARE
BEGIN
    INSERT INTO table_aux("VALUE") VALUES(1);
END;

CREATE OR REPLACE
TRIGGER trigger2
AFTER INSERT ON table_aux
FOR EACH ROW
DECLARE
    v_value NUMBER := 100;
    v_num   NUMBER := 10;
BEGIN
    UPDATE  table_update
    SET     "VALUE"  = v_value - v_num
    ,       FIELD_OK = 'OK'
    WHERE   ID = (SELECT MAX(ID)
                  FROM   TABLE_UPDATE TAB);
END;

INSERT INTO table_update VALUES (1, 1, 'OK');
INSERT INTO table_update VALUES (2, 1, 'OK');
INSERT INTO table_update VALUES (3, 1, 'OK');
INSERT INTO table_update VALUES (4, 1, 'OK');
INSERT INTO table_update VALUES (5, 1, 'OK');

SELECT * FROM table_update;
/*
1   90  OK
2   90  OK
3   90  OK
4   90  OK
5   1   OK
*/

SELECT * FROM table_aux;
/*
1
1
1
1
1
*/