views:

62

answers:

2

I've some prob with the function TABLE in ORACLE.

SET SERVEROUTPUT ON SIZE 100000;

DECLARE 

int_position NUMBER(20);

TYPE T_REC_EMP IS RECORD (  nameFile VARCHAR2(200) );    

R_EMP T_REC_EMP ; -- variable enregistrement de type T_REC_EMP

TYPE TAB_T_REC_EMP IS TABLE OF T_REC_EMP index by binary_integer ;
t_rec TAB_T_REC_EMP ; -- variable tableau d''enregistrements


PROCEDURE Pc_Insert ( v_value IN VARCHAR2) IS
BEGIN

  if t_rec.exists(t_rec.Last) then
    int_position := t_rec.last;
    int_position := int_position +1;

    t_rec(int_position).nomFichier := v_value;
  else
    t_rec(1).nomFichier :=v_value;
  end if;

END;

FUNCTION calice_ORACLE( n IN NUMBER) RETURN T_REC_EMP  PIPELINED IS

BEGIN

  FOR i in 1 .. n LOOP
    PIPE ROW(t_rec(i));
  END LOOP;

  RETURN;
END;

BEGIN

    Pc_Insert('allo1');
    Pc_Insert('allo2');
    Pc_Insert('allo3');

    SELECT * fROM TABLE(calice_ORACLE(2));

END;
/

I'm some error about function doesn't support in SQL statement ( I'm on 9i 9.2 vr)

+1  A: 
  • (as already pointed out in the comments) you have a SELECT statement embedded in PL/SQL with no instructions on what to do with the results of the query. You can either SELECT INTO a locally-declared variable, or you can LOOP through the results with a cursor, e.g. FOR rec IN (SELECT...) LOOP .. END LOOP;

  • Perhaps you want to create a PACKAGE instead of an anonymous block; then, in your calling program you can issue queries like your SELECT * FROM TABLE(mypackagename.calice_ORACLE(2)).

Jeffrey Kemp
+1  A: 

First of all you can't pipline assosiative arrays. Check this for more info on collection types. http://www.developer.com/db/article.php/10920%5F3379271%5F2/Oracle-Programming-with-PLSQL-Collections.htm

Second you need to select into or use cursor in pl/sql.

I wrote some demo code so that you can check a bit how it can work. I am not quite sure what you actually want to do but at least this compiles, which is good.

create or replace type t_rec_emp as object (namefile varchar2(200));    
/

create or replace type tab_t_rec_emp is table of t_rec_emp;
/

create or replace package mydemopack as
    t_rec tab_t_rec_emp := tab_t_rec_emp(); 
    procedure pc_insert ( v_value in varchar2);
    function calice_oracle( n in integer) return tab_t_rec_emp pipelined;

end;
/

create or replace package body mydemopack as
    procedure pc_insert ( v_value in varchar2) is
    begin
        t_rec.extend(1);
        t_rec(t_rec.count):= t_rec_emp(v_value);
    end;

    function calice_oracle( n in integer) return tab_t_rec_emp pipelined is

    begin

      for i in 1 .. n loop
        pipe row(t_rec(i));
      end loop;

      return;
    end;
end;
/


declare
    cursor c_cur is
        select * from table(myDemoPack.calice_oracle(2));
begin

    myDemoPack.pc_insert('allo1');
    myDemoPack.pc_insert('allo2');
    myDemoPack.pc_insert('allo3');

    for rec in c_cur loop
        dbms_output.put_line(rec.namefile);
    end loop;

end;
/
Calmar