Search code examples
sqloracle-databasedatatrigger

ORACLE TRIGGER CREATION


Hello I have a problem in the execution of this trigger that is before insertion or modification in the tables MATIERES check whether the number of hours in MATIERES is greater than the number of hours the MODULES...if this is the case i have to raise an exception ...can anyone help me please

CREATE OR REPLACE VerifNbrHeureMat
BEFORE INSERT OR UPDATE ON MATIERES
  FOR EACH ROW
    DECLARE
    D_EXCEPTION EXCEPTION
  BEGIN
    IF(:New.NBRHEURE_MAT > (Select nbr_heure FROM MODULES where (id_module=:New.ID_MODULE));)
        RAISE D_EXCEPTION
    END IF
  EXCEPTION
        WHEN D_EXCEPTION THEN
            RAISE_APPLICATION_ERROR(-20000,'Nbr heure matière Supérieur à Nbr Heure Module');
  END;

Solution

  • Try like this,

    CREATE OR REPLACE TRIGGER VerifNbrHeureMat
    BEFORE INSERT OR UPDATE ON MATIERES
    FOR EACH ROW
    DECLARE
         l_hour modules.nbr_heure%TYPE;
    BEGIN
         SELECT nbr_heure 
         INTO   l_hour
         FROM   modules 
         WHERE  id_module = :NEW.ID_MODULE;
    
         IF :NEW.NBRHEURE_MAT > l_hour THEN
              RAISE_APPLICATION_ERROR(-20000, 'Nbr heure matière Supérieur à Nbr Heure Module');
         END IF;
    END;