2013-11-21 99 views
0

我需要在歷史數據表中插入「記數」,當我插入「ALUGUER」的東西觸發器應在「HISTORICO」插入一行:觸發用於插入後

下了扳機:

create or replace 
trigger ADD_HISTORICO 
AFTER INSERT 
    ON ALUGUER 
    FOR EACH ROW 

DECLARE 
    cod_aluguer_p NUMBER(6,0); 
    cod_veiculo_p NUMBER(6,0); 
BEGIN 

    SELECT ID_ALUGUER INTO cod_aluguer_p 
    FROM ALUGUER; 
    SELECT COD_VEICULO INTO cod_veiculo_p 
    FROM ALUGUER; 

    INSERT INTO HISTORICO(ID_ENTRADA,DESCRICAO,DATA_REGISTO,NOVO_VEICULO,NOVO_ALUGUER) 
    VALUES(SEQ_HISTORICO.nextval,'NOVA DESCRIÇÃO','21/11/2013',cod_veiculo_p,cod_aluguer_p); 

END; 

錯誤報告:

SQL Error:
ORA-04091: BDDAD_DL1.ALUGUER table is mutating, trigger can not read or modify
ORA-06512: at "BDDAD_DL1.ADD_HISTORICO", line 6
ORA-04088: error during execution of trigger 'BDDAD_DL1.ADD_HISTORICO' 04091. 00000 -. "Table% s% s is mutating, trigger/function may not see it"
* Cause: A trigger (or a user defined plsql function that is referenced in
            this statement) attempted to look at (or modify) a table que was
            in the middle of being modified by the statement Which fired it.
* Action: Rewrite the trigger (or function) so it does not read that table.

回答

1

沒有SELECT是必需的,只是指:NEW值觸發。此外,您還可以使用TRUNC(SYSDATE),以獲取當前日期假設這是你的原意:

create or replace 
trigger ADD_HISTORICO 
AFTER INSERT 
    ON ALUGUER 
    FOR EACH ROW 

BEGIN 

    INSERT INTO HISTORICO(ID_ENTRADA,DESCRICAO,DATA_REGISTO, 
      NOVO_VEICULO,NOVO_ALUGUER) 
    VALUES(SEQ_HISTORICO.nextval,'NOVA DESCRIÇÃO', 
      TRUNC(SYSDATE),:NEW.COD_VEICULO ,:NEW.ID_ALUGUER); 

END; 
+0

謝謝您的時間。它幫助了很多。 – Vyward

2

您正在試圖讀取該觸發器已被觸發的表中的記錄。這是一個禁忌。所以,這個代碼:

SELECT ID_ALUGUER INTO cod_aluguer_p 
    FROM ALUGUER; 
    SELECT COD_VEICULO INTO cod_veiculo_p 
    FROM ALUGUER; 

是不允許的。此外,它沒有任何意義,因爲select中沒有WHERE子句,因此將返回所有行。模糊點,因爲無論如何您都無法做到這一點。你想要做的是引用:觸發行的新值istead。例如:

cod_aluguer_p := :new.ID_ALUGUER ; 
    cod_veiculo_p L= :NEW.COD_VEICULO; 

此外,您甚至不需要這些局部變量,只需使用:new直接。

INSERT INTO HISTORICO(ID_ENTRADA,DESCRICAO,DATA_REGISTO,NOVO_VEICULO,NOVO_ALUGUER) 
    VALUES(SEQ_HISTORICO.nextval,'NOVA DESCRIÇÃO','21/11/2013',:NEW.COD_VEICULO,:new.ID_ALUGUER); 

我會推薦閱讀Oracle docs on triggers

+0

謝謝你的時間。它幫助了很多。 – Vyward