I just started with triggers, I want the trigger to give me the msg error if the new tauxHoraire is 50% greater than the current tauxHoraire (so *1.5 >).
The trigger compiles but I’m getting ORA-04091: table name is mutating, trigger/function may not see it
. There might a problem in how I wrote it. Or is the problem somewhere else?
CREATE OR REPLACE TRIGGER restrictionTarifHoraire BEFORE UPDATE ON RESSOURCESPROJET FOR EACH ROW DECLARE taux_horaire RESSOURCESPROJET.prixHeure%TYPE; BEGIN Select ressourcesprojet.prixHeure into taux_horaire from ressourcesprojet where prixheure=:new.prixHeure; IF taux_horaire > (:NEW.prixHeure * 1.5) THEN RAISE_APPLICATION_ERROR(-20002, 'Err, new tauxHoraire cant be > than 50% of the current tauxHoraire'); END IF; END;
My test:
UPDATE RESSOURCESPROJET SET prixHeure = 800 WHERE RESSOURCESPROJET.idProjet=3;
Advertisement
Answer
Here is the trigger that will raise error when the prixHeure is greater then (1.5 * current prixHeure).
CREATE OR REPLACE TRIGGER restrictionTarifHoraire BEFORE UPDATE ON RESSOURCESPROJET FOR EACH ROW DECLARE taux_horaire RESSOURCESPROJET.prixHeure%TYPE; BEGIN IF :OLD.prixHeure*1.5 <:NEW.prixHeure THEN RAISE_APPLICATION_ERROR(-20002, 'Err, new tauxHoraire cant be > than 50% of the current tauxHoraire'); END IF; END;
Sample:
create table RESSOURCESPROJET (idProjet number,prixHeure number); insert into RESSOURCESPROJET values (3,100); UPDATE RESSOURCESPROJET SET prixHeure = 800 WHERE RESSOURCESPROJET.idProjet=3;
Error:
Error starting at line : 68 in command - UPDATE RESSOURCESPROJET SET prixHeure = 800 WHERE RESSOURCESPROJET.idProjet=3 Error report - ORA-20002: Err, new tauxHoraire cant be > than 50% of the current tauxHoraire ORA-06512: at "RESTRICTIONTARIFHORAIRE", line 7 ORA-04088: error during execution of trigger 'RESTRICTIONTARIFHORAIRE'