<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>blog.watashii &#187; function</title>
	<atom:link href="http://blog.watashii.com/tag/function/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.watashii.com</link>
	<description></description>
	<lastBuildDate>Wed, 02 Nov 2011 02:23:46 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>PL/SQL &#8211; Pipelined Table Function</title>
		<link>http://blog.watashii.com/2008/07/plsql-pipelined-table-function/</link>
		<comments>http://blog.watashii.com/2008/07/plsql-pipelined-table-function/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 13:16:33 +0000</pubDate>
		<dc:creator>watashii</dc:creator>
				<category><![CDATA[Database]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[function]]></category>
		<category><![CDATA[oracle]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[pipelined]]></category>

		<guid isPermaLink="false">http://blog.watashii.com/?p=22</guid>
		<description><![CDATA[Normally when writing PL/SQL functions, they return a single value result.  But what if you want to return a collection type instead?  The easiest way to do this is via a pipelined function, with the PIPELINED keyword, in which rows are returned by the function iteratively.   It can be created at the schema level, or [...]


Related Posts:<ol><li><a href='http://blog.watashii.com/2008/08/oracle-sql-loader-importing-csv-files-into-a-table/' rel='bookmark' title='Permanent Link: Oracle SQL Loader &#8211; Importing CSV files to a table'>Oracle SQL Loader &#8211; Importing CSV files to a table</a></li>
<li><a href='http://blog.watashii.com/2008/09/oracle-null-indexing/' rel='bookmark' title='Permanent Link: Oracle Index Null Values'>Oracle Index Null Values</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<p>Normally when writing PL/SQL functions, they return a single value result.  But what if you want to return a collection type instead?  The easiest way to do this is via a pipelined function, with the PIPELINED keyword, in which rows are returned by the function iteratively.   It can be created at the schema level, or inside a package.  I prefer the latter since its more portable, and easier to manage.</p>
<p>Inside the function, you return individual elements of the collection type, instead of returning the entire collection type when function ends.  This also has performance benefits, depending on your application.</p>
<p><span id="more-22"></span></p>
<p>The code below defines a custom number collection type and a pipelined function; within a package / package body.</p>
<pre name="code" class="sql">
CREATE PACKAGE mypackage AS
  TYPE num_type IS TABLE OF NUMBER;
  FUNCTION myfunction (x NUMBER) RETURN num_type PIPELINED;
END mypackage;
/

CREATE PACKAGE BODY mypackage AS
  -- myfunction returns a collection of elements (1,2,3,... x)
  FUNCTION myfunction (x NUMBER) RETURN num_type PIPELINED IS
    BEGIN
      FOR i IN 1..x LOOP
        -- each number is returned per iteration
        PIPE ROW(i);
      END LOOP;
      RETURN;
    END;
END mypackage;
/
</pre>
<p>To call the function, simply call:</p>
<pre name="code" class="sql">
SELECT * FROM TABLE(mypackage.myfunction(5));
</pre>
<p>Which results in:<br />
<code><br />
COLUMN_VALUE<br />
------------<br />
           1<br />
           2<br />
           3<br />
           4<br />
           5<br />
</code></p>


<p>Related Posts:<ol><li><a href='http://blog.watashii.com/2008/08/oracle-sql-loader-importing-csv-files-into-a-table/' rel='bookmark' title='Permanent Link: Oracle SQL Loader &#8211; Importing CSV files to a table'>Oracle SQL Loader &#8211; Importing CSV files to a table</a></li>
<li><a href='http://blog.watashii.com/2008/09/oracle-null-indexing/' rel='bookmark' title='Permanent Link: Oracle Index Null Values'>Oracle Index Null Values</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://blog.watashii.com/2008/07/plsql-pipelined-table-function/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

