dbTalk Databases Forums  

How to create a table valued function in oracle

comp.database.oracle comp.database.oracle


Discuss How to create a table valued function in oracle in the comp.database.oracle forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Mike D
 
Posts: n/a

Default How to create a table valued function in oracle - 01-19-2004 , 12:17 AM






I would like to create a multi-statement table valued function in Oracle
(having done so for years in SQL Server). Functions in Oracle do not allow
a table return type, so I was wondering if someone could suggest a
workaround to replicate the same functionality?

Thanks

Mike



Reply With Quote
  #2  
Old   
elwood
 
Posts: n/a

Default Re: How to create a table valued function in oracle - 01-19-2004 , 08:41 AM






"Mike D" <miked (AT) hotmail (DOT) com> wrote

Quote:
I would like to create a multi-statement table valued function in Oracle
(having done so for years in SQL Server). Functions in Oracle do not allow
a table return type, so I was wondering if someone could suggest a
workaround to replicate the same functionality?

Thanks

Mike
Oracle PL/SQL provides self-declared table types. Create a package
containing your function. Inside the package, declare a table type
that you use as a return type:

create or replace package my_pak
as
-- example: a list of strings
type mytabtype is table of varchar2(42) index by binary_integer;

function tablefunc(n number) return my_pak.mytabtype;
end my_pak;

create or replace package body my_pak
as
function tablefunc(n number)
return my_pak.mytabtype
is
retval my_pak.mytabtype;
begin
for z in (select name, id from characters where zaphod_id = n)
loop
retval(z.id) := z.name;
end loop;
return retval;
end;
end my_pak;

In other PL/SQL packages, simply you this special data type
my_pak.mytabtype.
Note that there are more types:
- tables which are not indexed by binary integers; these may be
referred to by SQL statements
- VARRAYs: these have a fixed length but provide better performance
- PL/SQL tables may be inserted into single record fields if they are
treated as objects

See also: Oracle Documentation / Application Development / PL/SQL
developer's guide

greetinx
elwood



Ät inte gul snö :=)


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.