Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.misc -> Re: PL/SQL Table as Result of stored Function
You cannot use a function that returns a PL/SQL table
in a select statement. Here's the doc:
To be callable from SQL statements, a stored function must meet several requirements, one of which is that its arguments have SQL datatypes such as CHAR, DATE, or NUMBER. None of the arguments can have non-SQL types such as BOOLEAN, TABLE, or RECORD.
But you can do something like this:
v734>select * from t;
CODE PRICE_TYPE COST
---------- ---------- ----------
1 t10 10 2 t20 20 10 t100 100 10 t101 200
4 rows selected.
v734>declare
2
3 type PRICE_RecType IS RECORD (
4 price_type varchar2(10) := '',
5 cost number := 0);
6
7 TYPE PRICE_TabTyp IS TABLE OF PRICE_RecType
8 INDEX BY BINARY_INTEGER;
9
10 price_tab price_tabtyp;
11
12 function GetPriceInfo(code in number) RETURN
13 PRICE_TabTyp IS
14 PRICE_tab PRICE_TabTyp;
15 cursor c (p_code number) is
16 select price_type, cost from t where code = p_code;
17 i integer := 0;
18 price_type t.price_type%type;
19 cost t.cost%type;
20 BEGIN
21
22 open c (code);
23 loop
24 i := i+1; 25 fetch c into price_type, cost; 26 exit when c%notfound; 27 price_tab(i).price_type := price_type; 28 price_tab(i).cost := cost;
42 for i in 1..price_tab.count loop 43 dbms_output.put_line('Type: '||price_tab(i).price_type|| 44 ', cost: '||price_tab(i).cost); 45 end loop;
PL/SQL procedure successfully completed.
or like this
v734>create or replace package my_pkg is
2
3 type PRICE_RecType IS RECORD (
4 price_type varchar2(10) := '', 5 cost number := 0 6 );
Package created.
v734>create or replace package body my_pkg is
2
3 function GetPriceInfo(code in number) RETURN PRICE_TabTyp
4 is
5 PRICE_tab PRICE_TabTyp;
6 cursor c (p_code number) is
7 select price_type, cost from t where code = p_code;
8 i integer := 0;
9 price_type t.price_type%type;
10 cost t.cost%type;
11 BEGIN
12 open c (code);
13 loop
14 i := i+1; 15 fetch c into price_type, cost; 16 exit when c%notfound; 17 price_tab(i).price_type := price_type; 18 price_tab(i).cost := cost;
Package body created.
v734>declare
2 price_tab my_pkg.price_tabtyp; 3 cursor c is select distinct code from t;4 begin
8 for rec in c loop 9 price_tab := my_pkg.getpriceinfo(rec.code); 10 dbms_output.put_line ('Code: '||rec.code); 11 for i in 1..price_tab.count loop 12 dbms_output.put_line('- Type: '||price_tab(i).price_type|| 13 ', cost: '||price_tab(i).cost); 14 end loop; 15 end loop;
PL/SQL procedure successfully completed.
Hope this will help.
jeremy twiggs a écrit dans le message
<01befdce$319521e0$0b0410ac_at_jeremy.hotelscene.co.uk>...
>Help, can anybody point me in the right direction?
>
>Can I write a SQL statement that calls a stored PL/SQL function
>that uses a PL/SQL table for its return parameter?
>
>i.e:
>
>TYPE PRICE_RecType IS RECORD (
> price_type varchar2(10) := '',
> cost number(5,5) := 0);
>
>TYPE PRICE_TabTyp IS TABLE OF PRICE_RecType
> INDEX BY BINARY_INTEGER;
>
>CREATE or REPLACE function GetPriceInfo(code in number(6)) RETURN
>PRICE_TabType IS
> PRICE_tab PRICE_TabTyp;
>BEGIN
>
> blah, blah, blah...
>
> RETURN PRICE_tab;
>
>END;
>
>then use it like
>
>SELECT
> a.code,
> GetPriceInfo (a.code)
>FROM
> a_table a
>
>such that it will return the columns
>code, price_type and cost.
>
>
>
Received on Mon Sep 13 1999 - 07:07:40 CDT
![]() |
![]() |