Search code examples
oracleplsqldatabase-trigger

Nullify an inserting value in a row trigger


I have a problem with my database trigger in which I'm supposed to nullify the inserted/updated value (RESULTATOBT), although I need the OLD/NEW variables, so I can't apply this solution (updating the table in the table trigger).

The error is a mutating table one; due to modifying a table in a for each row trigger.

Here's the concerned extract of my code:

CREATE OR REPLACE TRIGGER VERIF_RESULTATS
  AFTER INSERT OR UPDATE OF RESULTATOBT
  ON PARTICIPATION
  FOR EACH ROW
  FOLLOWS VERIF_PARTICIPATION
  WHEN ( NEW.RESULTATOBT IS NOT NULL )
BEGIN 
  UPDATE PARTICIPATION p
  SET RESULTATOBT = NULL
  WHERE p.CDPERS = :NEW.CDPERS
    AND p.CDCOMPET = :NEW.CDCOMPET;
END;
/

Thank you in advance for your help.

N.B.: I'm a relatively new member, so I might require additional advices/edits if my question doesn't fit the proper format.


Solution

  • Use a before trigger and simply assign NULL:

    CREATE OR REPLACE TRIGGER VERIF_RESULTATS
      BEFORE INSERT OR UPDATE OF RESULTATOBT
      ON PARTICIPATION
      FOR EACH ROW
      WHEN ( NEW.RESULTATOBT IS NOT NULL )
    BEGIN 
      :new.RESULTATOBT := NULL;
    END;
    /