Oracle
 sql >> Teknologi Basis Data >  >> RDS >> Oracle

Kembalikan tabel dalam fungsi PL/SQL

Sintaks yang Anda gunakan tentu saja sesuatu yang tidak didukung di Oracle PLSQL . Di Oracle PLSQL Anda perlu melakukan sesuatu seperti:

-- 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;
/

Atau sesuai @a_horse_with_no_name, Menggunakan Fungsi PipeLine, bisa seperti di bawah ini:

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;
/



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. Fungsi TRUNC(tanggal) di Oracle

  2. Cara menyisipkan string yang berisi &

  3. Oracle SQL Menemukan 5 gaji terendah

  4. Tidak ada acara onMessage di Runtime (Java JMS MessageListener di Oracle Queue)

  5. Pesanan Kustom di Oracle SQL