0

我有一个触发器,它更新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_UPDATE与 中使用的表相同trigger2。变量V_VALUEV_NUM使用的trigger1NUMBER变量,value列是NUMBER(30,20)

4

2 回答 2

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
*/
于 2013-06-10T13:29:42.567 回答
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

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

于 2013-06-10T12:20:03.487 回答