尝试在表格中插入一行并创建触发器后出现这些错误

时间:2019-01-12 21:44:38

标签: oracle plsql

我遇到这些错误:

INSERT INTO "DANIEL"."STATION" (NUM_STATION, NOM_STATION, ALTITUDE, REGION) 
VALUES ('6', 'Baalbak', '1250', 'Baalbek')
ORA-04091: table DANIEL.STATION is mutating, trigger/function may not see it
ORA-06512: at "DANIEL.TRUPDATE_HIST_STATION", line 5
ORA-04088: error during execution of trigger 'DANIEL.TRUPDATE_HIST_STATION'
ORA-06512: at line 1


One error saving changes to table "DANIEL"."STATION":
Row 6: ORA-04091: table DANIEL.STATION is mutating, trigger/function may not 
see it
ORA-06512: at "DANIEL.TRUPDATE_HIST_STATION", line 5
ORA-04088: error during execution of trigger 'DANIEL.TRUPDATE_HIST_STATION'
ORA-06512: at line 1

在表格站中插入一行

这是我刚刚编写的触发器的代码:

create or replace TRIGGER trupdate_hist_station
  AFTER INSERT ON STATION
  FOR EACH ROW
DECLARE
  V_num_station NUMBER;
BEGIN
  BEGIN
    SELECT s.NUM_STATION
      INTO v_num_station
      FROM STATION s
      WHERE s.NUM_STATION = :NEW.NUM_STATION;
  EXCEPTION
    WHEN no_data_found THEN
      v_num_station :=0;
  END;

 INSERT INTO HIST_STATION
    ("NUM_STATION","ANNEE","NB_RESERV","REVENU")
  VALUES
    (V_num_station,(SELECT to_char(SYSDATE,'YYYY')FROM DUAL),0,0);
END trupdate_hist_station;

在表Station中添加一行后,我只需要在Hist_table中添加新行

1 个答案:

答案 0 :(得分:2)

无需SELECT插入表中的任何内容(这就是导致 mutation 的原因)。

此外,摆脱双引号,因为列名仍默认为大写,所以不必使用双引号。

此外,不要从SELECT dual年开始-SYSDATE提取

这应该做:

create or replace TRIGGER trupdate_hist_station
  AFTER INSERT ON STATION
  FOR EACH ROW
BEGIN
 INSERT INTO HIST_STATION  
    (num_station, annee, nb_reserv, revenu)
 VALUES
    (:new.num_station, extract (year from sysdate), 0, 0);
END trupdate_hist_station;