小编典典

Oracle SQL触发器上的突变表

sql

我正在尝试执行一个触发器,但是出现了变异表错误。SQL代码是这样的:

CREATE OR REPLACE TRIGGER CHK_Apartado_D
BEFORE INSERT OR UPDATE ON CONTRACTS
FOR EACH ROW
DECLARE 
errorvisualizacion EXCEPTION;
local_enddate DATE;
BEGIN
  SELECT enddate INTO local_enddate FROM CONTRACTS WHERE clientid=:new.clientid;
  IF local_enddate > SYSDATE OR local_enddate IS NULL
  THEN 
    UPDATE CONTRACTS SET enddate = SYSDATE - 1 WHERE clientid=:new.clientid;
  END IF;
END CHK_Apartado_B;
/

我得到的错误是这样的:

Informe de error -
Error SQL: ORA-04091: table HR.CONTRACTS is mutating, trigger/function may not see it
ORA-06512: at "HR.CHK_APARTADO_D", line 5
ORA-04088: error during execution of trigger 'HR.CHK_APARTADO_D'
ORA-06512: at "HR.CHK_APARTADO_D", line 8
ORA-04088: error during execution of trigger 'HR.CHK_APARTADO_D'
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 that 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.

当我插入新合同时,我必须检查该客户在实际日期中是否还有其他合同,如果他有,我必须将截止日期合同更新为昨天并让新INSERT。那么,我该怎么做以防止表格发生突变?


阅读 168

收藏
2021-04-15

共1个答案

小编典典

您的触发器在更新或插入CONTRACTS时触发,然后尝试更新CONTRACTS,从而触发该触发器…。看到问题了吗?

您需要计算结束日期,然后实际执行插入/更新。

2021-04-15