SHARE
Facebook X Pinterest WhatsApp

Pipelined Table Function

Mar 13, 2003

>>Script Language and Platform: Oracle PL/SQL
The purpose of this code is to provide an example of the new pipelined and parallel PL/SQL table functions available in Oracle9i Database.

Author: Claudiu Ariton

Script Code: create table phones
(cust_id number(12) primary key,
phone1 varchar2(50),
phone2 varchar2(50),
phone3 varchar2(50),
phone4 varchar2(50),
phone5 varchar2(50)
)
/insert data into table
begin
 for i in 1..1000000 loop
  insert into phones values (i,i+1,i+2,i+3,i+4,i+5);
  if mod(i,1000)=0 then
    commit;
  end if;
 end loop;
 commit;
end;
/
create or replace package TypePkg is
 type PhoneRecord is RECORD
(cust_id number(12),
 phone1 varchar2(50),
 phone2 varchar2(50),
 phone3 varchar2(50),
 phone4 varchar2(50),
 phone5 varchar2(50));
 type PhoneCursorType is REF CURSOR return PhoneRecord;
end;
/
create or replace type PhoneResult as object
( cust_id  number(12),
phone varchar2(50) );
/
create or replace type PhoneResultTab as table of PhoneResult;
/
create or replace function GetPhones (inrecs IN TypePkg.PhoneCursorType)
return PhoneResultTab pipelined
order inrecs by ( cust_id )
parallel_enable ( partition inrecs by Hash ( cust_id ) )
as
 currec PhoneResult := PhoneResult(null, null);
 initrec TypePkg.PhoneRecord;
begin
loop
 fetch inrecs into initrec.cust_id, initrec.phone1, initrec.phone2,
initrec.phone3, initrec.phone4, initrec.phone5;
 exit when inrecs%NOTFOUND;  
 currec.cust_id:=initrec.cust_id;
 if initrec.phone1 is not null then
  currec.phone:=initrec.phone1;
  pipe row(currec);
 end if;
 if initrec.phone2 is not null then
  currec.phone:=initrec.phone2;
  pipe row(currec);
 end if;
 if initrec.phone3 is not null then
  currec.phone:=initrec.phone3;
  pipe row(currec);
 end if;
 if initrec.phone4 is not null then
  currec.phone:=initrec.phone4;
  pipe row(currec);
 end if;
 if initrec.phone5 is not null then
  currec.phone:=initrec.phone5;
  pipe row(currec);
 end if;
end loop;
close inrecs;
return;    
end;
/
*****************************************************************
Test: 
select * from
TABLE(CAST(GetPhones(cursor(select
cust_id,phone1,phone2,phone3,phone4,phone5 from phones)) AS
PhoneResultTab));
/ 
select /*+ USE_NL(vt) */ cust_id,phone from
TABLE(CAST(GetPhones(cursor(select
cust_id,phone1,phone2,phone3,phone4,phone5 from phones)) AS
PhoneResultTab)) vt
where exists
(select /*+ USE_NL(p) */ cust_id,phone1 from phones p where
p.cust_id=vt.cust_id and p.PHONE1=vt.phone)
/



Disclaimer:
We hope that the information on these script pages is
valuable to you. Your use of the information contained in these pages,
however, is at your sole risk. All information on these pages is provided
“as -is”, without any warranty, whether express or implied, of its accuracy,
completeness, or fitness for a particular purpose…

Disclaimer Continued

Back to Database Journal Home

Recommended for you...

What Backups Do I Have?
Gregory Larsen
May 12, 2021
Improving the Performance of a Table Variable using Optimizer Hint RECOMPILE
Gregory Larsen
Apr 1, 2021
TYPE Definition Change in Oracle 21c
Tip 74 – Changing Cost Threshold for Parallelism
Gregory Larsen
Feb 24, 2021
Database Journal Logo

DatabaseJournal.com publishes relevant, up-to-date and pragmatic articles on the use of database hardware and management tools and serves as a forum for professional knowledge about proprietary, open source and cloud-based databases--foundational technology for all IT systems. We publish insightful articles about new products, best practices and trends; readers help each other out on various database questions and problems. Database management systems (DBMS) and database security processes are also key areas of focus at DatabaseJournal.com.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.