Advertisement
cesarcardinale

M3106C BDA - TP2

Sep 20th, 2018
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PL/SQL 2.30 KB | None | 0 0
  1. /* Exercice n°1 */
  2.   /* Question n°1 */
  3.   CREATE OR REPLACE TRIGGER createControle BEFORE INSERT OR UPDATE OF TYPEC, DATEDEB, DATEFIN, NOTEMAX ON CONTROLE
  4.   FOR EACH ROW
  5.   BEGIN
  6.     IF :NEW.DATEFIN IS NULL OR :NEW.TYPEC IN ('Test', 'Test rattrapage', 'Interrogation')
  7.     THEN :NEW.DATEFIN := :NEW.DATEDEB;
  8.     END IF;
  9.     IF :NEW.NOTEMAX IS NULL
  10.     THEN :NEW.NOTEMAX := 20;
  11.     END IF;
  12.   END;
  13.  
  14.   /* Question 2 */
  15.   CREATE OR REPLACE TRIGGER calculNoteMaxDef BEFORE INSERT OR UPDATE OF NOTEDEF, NOTE, NOTEMAX ON NOTATION
  16.   FOR EACH ROW
  17.   DECLARE MAX := CONTROLE.NOTEMAX%TYPE;
  18.   BEGIN
  19.     SELECT NOTEMAX INTO MAX FROM CONTROLE WHERE :NEW.IDC := CONTROLE.IDC, :NEW.NOTEDEF := (:NEW.NOTE * 20)/MAX;
  20.   EXCEPTION
  21.     WHEN NO_DATA_FOUND THEN RAISE_APPLICATION_ERROR(-20001, 'Pas de controle ayant cet identifiant.')
  22.   END;
  23.  
  24. /* Exercice n°2 */
  25.   /* Question n°1 */
  26.   CREATE OR REPLACE TRIGGER verifNoEmprunt BEFORE INSERT ON EMPRUNT
  27.   FOR EACH ROW
  28.   DECLARE TOTAL ABONNE.NBEMPRUNT%TYPE;
  29.   BEGIN
  30.     SELECT NBEMPRUNT INTO TOTAL FROM ABONNE WHERE IDA = :NEW.IDA;
  31.     IF TOTAL >= 5
  32.     THEN RAISE_APPLICATION_ERROR (-20001, 'Trop demprunts.');
  33.     END IF;
  34.   END;
  35.  
  36.   /* Question n°2 */
  37.   CREATE OR REPLACE TRIGGER updateEmprunt AFTER INSERT OR REPLACE OF NBEMPRUNT ON ABONNE
  38.   FOR EACH ROW
  39.   DECLARE NB NUMBER(2);
  40.   BEGIN
  41.     IF DATERET IS NULL
  42.     THEN NB := NB + 1;
  43.     ELSE NB := NB - 1;
  44.     END IF;
  45.     UPDATE ABONNE SET NBEMPRUNT := NBEMPRUNT + NB WHERE IDA = :NEW.IDA;
  46.   END;
  47.  
  48.   /* Question n°3 */
  49.   CREATE OR REPLACE TRIGGER retourLivre AFTER UPDATE OF DATERET ON EMPRUNT
  50.   FOR EACH ROW
  51.   DECLARE
  52.   CURSOR c_reserv IS SELECT * FROM RESERV WHERE CONTACT := 'NON' AND IDL IN (SELECT IDL FROM EXEMPLAIRE WHERE CODBAR = :=NEW.CODBAR) ORDER BY DATER;
  53.   NOMA ABONNE.NOM%TYPE;
  54.   PRENOMA ABONNE.PRENOM%TYPE;
  55.   ADRESSEA ABONNE.ADRESSE%TYPE;
  56.   TITREL LIVRE.TITRE%TYPE;
  57.   CR c_reserv%ROWTYPE;
  58.   BEGIN
  59.     OPEN c_reserv;
  60.     FETCH c_reserv INTO CR
  61.     IF c_reserv%FOUND
  62.     THEN SELECT NOM, PRENOM INTO NOMA, PRENOMA FROM ABONNE WHERE IDA = :CR.IDA;
  63.          SELECT TITRE INTO TITREL FROM LIVRE WHERE IDL = :CR.IDL;
  64.          AVERTIR(NOMA, PRENOMA, ADRESSEA, TITREL);
  65.          UPDATE RESERV SET CONTACT := 'OUI' WHERE IDA = :CR.IDA AND IDL = :CR.IDL;
  66.     ELSE CLOSE c_reserv;
  67.     END IF;
  68.   END;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement