{"id":995,"date":"2015-10-14T14:40:56","date_gmt":"2015-10-14T14:40:56","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=995"},"modified":"2015-10-15T13:18:38","modified_gmt":"2015-10-15T13:18:38","slug":"pipe-row-oracle-function-example","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/pipe-row-oracle-function-example\/","title":{"rendered":"PIPE ROW Oracle Function Example"},"content":{"rendered":"<p>Table Function return a result set that mimics what we would normally expect from a traditional SQL SELECT statement<\/p>\n<p>Table functions are a new feature in Oracle9i that allow you to define a set of PL\/SQL statements that will, when queried, behave just as a regular query to table would. The added benefit to having a table function is that you can perform transformations to the data in question before it is returned in the result set. <\/p>\n<p><strong>CREATE OBJECT<\/strong><br \/>\n Create our own object type called PERSON_DETAILS.Then we create a table of PERSON_DETAILS called PERSON_DETAILS_TABLE.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE TYPE PERSON_DETAILS AS OBJECT\r\n       (USER_NAME     VARCHAR2(50),\r\n        ADDRESS       VARCHAR2(50),\r\n        LOCATION      VARCHAR2(50));\r\n\/\r\nCREATE TYPE PERSON_DETAILS_TABLE AS TABLE OF PERSON_DETAILS;\r\n\/\r\n<\/pre>\n<p><strong>PIPELINED Clause<\/strong><\/p>\n<p>Within the CREATE FUNCTION clause, there is a new option called PIPELINED. This option tells Oracle to return the results of the function as they are processed, and not wait for a complete execution or completion of the result set. This pipelining of the result set to one row at a time has the immediate advantage of not requiring excessive memory or disk staging resources.<br \/>\nPIPE ROW(out_rec)<\/p>\n<p>The PIPE ROW statement is the interface or mechanism to send a piped row through the PIPELINED option through to the caller of the function.<\/p>\n<p>Working with a simple pipelined function requires 2 things<\/p>\n<ul>\n<li>collection type<\/li>\n<li>pipelined function<\/li>\n<\/ul>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n CREATE OR REPLACE TYPE number_ntt AS TABLE OF NUMBER;\r\n<\/pre>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE FUNCTION row_generator(rows_in IN P_INTEGER) RETURN number_ntt\r\n  PIPELINED IS\r\nBEGIN\r\n  FOR i IN 1 .. rows_in LOOP\r\n    PIPE ROW(i);\r\n  END LOOP;\r\n  RETURN;\r\nEND;\r\n\/\r\n<\/pre>\n<p>The <strong>CSV_TABLE<\/strong> is a collection which has rows of comma separated value.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nCREATE OR REPLACE TYPE &quot;CSV_TABLE&quot; as table of varchar2(32767)\r\nCSV_TABLE\r\n<\/pre>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nFUNCTION CSV_TO_TABLE(p_delimted_string VARCHAR2,\r\n                         p_delimter        VARCHAR2 := ',')\r\n  RETURN CSV_TABLE\r\n  PIPELINED IS\r\n  indexCount PLS_INTEGER;\r\n  csvString  VARCHAR2(32767) := p_delimted_string;\r\nBEGIN\r\n  LOOP\r\n    indexCount := instr(csvString, p_delimter);\r\n  \r\n    IF indexCount &gt; 0 THEN\r\n      PIPE ROW(substr(csvString, 1, indexCount - 1));\r\n      csvString := substr(csvString, indexCount + length(p_delimter));\r\n    ELSE\r\n      PIPE ROW(csvString);\r\n      EXIT;\r\n    END IF;\r\n  \r\n  END LOOP;\r\n  RETURN;\r\nEND CSV_TO_TABLE;\r\n<\/pre>\n<p><strong>Input<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nA,B,C,D\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\nA\r\nB\r\nC\r\nD\r\n<\/pre>\n<p>The above output is a collection.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\r\n SELECT PACKAGE_NAME.CSV_TO_TABLE('A,B,C,D') FROM DUAL;\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Table Function return a result set that mimics what we would normally expect from a traditional SQL SELECT statement Table functions are a new feature in Oracle9i that allow you to define a set of PL\/SQL statements that will, when queried, behave just as a regular query to table would. The added benefit to having&hellip; <a href=\"https:\/\/codethataint.com\/blog\/pipe-row-oracle-function-example\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[75],"tags":[],"class_list":["post-995","post","type-post","status-publish","format-standard","hentry","category-packages-procedures"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/995","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=995"}],"version-history":[{"count":7,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/995\/revisions"}],"predecessor-version":[{"id":1008,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/995\/revisions\/1008"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=995"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=995"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=995"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}