L3 Info : SGBD
 
◃  Ch. 11 Oracle PL/SQL  ▹
 

Fonctions Tables

  • Création d'un type associé à un enregistrement retourné 
    CREATE TYPE rec_livre AS OBJECT(liv_num INTEGER, liv_titre VARCHAR2(50)); 
  • Création d'un type table constituée des enregistrements précédents :
    CREATE TYPE tab_livre as TABLE OF rec_livre;
  • Création de la fonction table : utilisation du mot-clé PIPELINED et de la commande PIPE ROW
    CREATE OR REPLACE FUNCTION listeLivre(debut IN integer, fin IN INTEGER) 
      RETURN  tab_livre PIPELINED 
    IS
      varLivre rec_livre := rec_livre(NULL, NULL);
    BEGIN
      FOR ligne IN (SELECT * FROM livre WHERE liv_num >= debut AND liv_num <= fin) LOOP
            varLivre.liv_num := ligne.liv_num;
            varLivre.liv_titre := ligne.liv_titre;
    	PIPE ROW (varLivre);
      END LOOP;
      RETURN;
    END;
    /
  • Utilisation de la fonction table :
    SELECT * FROM TABLE(listelivre(2,6));
  • Résultat :
    liv_num liv_titre
    2	Le Voyageur de l'Inconnu
    3	Les Mutants
    4	La Galaxie Noire
    5	La Route Étoilée
    6	Les Courants de l'Espace