Oracle
 sql >> Database >  >> RDS >> Oracle

Restituisce una tabella in una funzione PL/SQL

La sintassi che usi è sicuramente qualcosa che non è supportato in Oracle PLSQL . In Oracle PLSQL devi fare qualcosa come:

-- Create Object of your table
CREATE TYPE TABLE_RES_OBJ AS OBJECT (
     IDINGREDIENT                  INT ,
     NOMINGREDIENT                 VARCHAR (255) ,
     QUANTITE                      INT 
);

--Create a type of your object 
CREATE TYPE TABLE_RES AS TABLE OF TABLE_RES_OBJ;
/

--Function Use the type created as Return Type
CREATE OR REPLACE FUNCTION CHANGENBPERSONNES (
     RECETTE      IN   INT,
     NBPERSONNE   IN   INT)
     RETURN TABLE_RES
AS
     CURSOR CURSEUR_ETAPE
     IS
          SELECT  TABLE_RES_OBJ (IR.*)
            FROM INGREDIENTRECETTE IR 
            JOIN RECETTE R ON IR.IDRECETTE =R.IDRECETTE
                 JOIN INGREDIENT I ON IR.IDINGREDIENT = I.IDINGREDIENT
           WHERE R.IDRECETTE = RECETTE;

     VAR       TABLE_RES:= TABLE_RES();
BEGIN
     OPEN CURSEUR_ETAPE;

     LOOP
          FETCH CURSEUR_ETAPE
          BULK COLLECT INTO VAR LIMIT 100;

          EXIT WHEN CURSEUR_ETAPE%NOTFOUND;
     END LOOP;

     CLOSE CURSEUR_ETAPE;

     RETURN VAR;
END;
/

O secondo @a_horse_with_no_name, utilizzando la funzione PipeLine, potrebbe essere il seguente:

CREATE OR REPLACE FUNCTION CHANGENBPERSONNES (RECETTE      IN INT,
                                              NBPERSONNE   IN INT)
   RETURN TABLE_RES
   PIPELINED
AS
   CURSOR CURSEUR_ETAPE
   IS
      SELECT *
        FROM INGREDIENTRECETTE IR
             JOIN RECETTE R ON IR.IDRECETTE = R.IDRECETTE
             JOIN INGREDIENT I ON IR.IDINGREDIENT = I.IDINGREDIENT
       WHERE R.IDRECETTE = RECETTE;
BEGIN
   FOR i IN CURSEUR_ETAPE
   LOOP
      PIPE ROW (TABLE_RES_OBJ (i.idingredient, i.Nom, i.quantite));
      EXIT WHEN CURSEUR_ETAPE%NOTFOUND;
   END LOOP;

   RETURN;
END;
/