L3 Info : SGBD
 
◃  Ch. 10 PL/pgSQL  ▹
 

Trigger annulant les insertions

  • Code :
    CREATE FUNCTION trigOutlaw_3() RETURNS trigger AS $$
    DECLARE
    	nbol int;
    BEGIN
    	SELECT COUNT(*) INTO nbol FROM outlaw;
    	RAISE NOTICE '% outlaws dans la table', nbol;
    	RETURN NULL;
    END $$ LANGUAGE plpgsql;
    
    CREATE TRIGGER trigOL
      BEFORE INSERT ON outlaw FOR EACH ROW
      EXECUTE PROCEDURE trigOutlaw_3();
    
    DELETE FROM outlaw;
    INSERT INTO outlaw(ol_nom,ol_prenom,ol_naissance) SELECT etud_nom, etud_prenom, etud_naissance FROM etudiant WHERE etud_nom = 'Dalton';
    SELECT * FROM outlaw;
  • Trace d'exécution :
    DELETE 4
    psql:fichier.sql:102: NOTICE:  0 outlaws dans la table
    psql:fichier.sql:102: NOTICE:  0 outlaws dans la table
    psql:fichier.sql:102: NOTICE:  0 outlaws dans la table
    psql:fichier.sql:102: NOTICE:  0 outlaws dans la table
    INSERT 0 0
     ol_id | ol_nom | ol_prenom | ol_naissance 
    -------+--------+-----------+--------------
    (0 rows)