Question

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;
Was it helpful?

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;

OTHER TIPS

You can do it like the below using SELECT INTO

CREATE OR REPLACE VerifNbrHeureMat
BEFORE INSERT OR UPDATE ON MATIERES
FOR EACH ROW
DECLARE
D_EXCEPTION EXCEPTION;
l_nbr_heure  NUMBER;
BEGIN
  Select nbr_heure into l_nbr_heure FROM MODULES 
  where id_module=:NEW.ID_MODULE;
IF(:NEW.NBRHEURE_MAT > l_nbr_heure)
Then
    RAISE D_EXCEPTION;
END IF;
EXCEPTION
    WHEN D_EXCEPTION THEN
        RAISE_APPLICATION_ERROR(-20000,'Nbr heure matière Supérieur à Nbr Heure Module');
END;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top