Oracle implements the Split function function

Source: Internet
Author: User



Reprint: http://blog.csdn.net/jojo52013145/article/details/6758279
In an actual application, in order for the PL/SQL function to return multiple rows of data, it must be done by returning a REF CURSOR or a . This case of REF CURSOR is limited to the data that can be selected from the query, and the entire collection must be materialized before it can be returned. 9i corrects the latter case by introducing a pipelined table function. A table function is a function that returns the entire set of rows (usually as a collection), which can be queried directly from an SQL statement as if it were a real database table. The pipelined table function is similar, but it returns the data as it was built, rather than returning it all at once. pipelining table functions are more efficient because the data can be returned as quickly as possible.



The pipelined table function must return a collection. In the function, the PIPE ROW statement is used to return a single element of the collection, and the function must end with an empty return statement to indicate that it has completed. Once we have created the above function, we can invoke it from the SQL query using the TABLE operator.



1. Define a type of object
CREATE OR replace TYPE ty_obj as OBJECT (str1 VARCHAR2 (50));
2. Define a type that inherits the Ty_obj;
CREATE OR replace TYPE Strsplit_type as TABLE of Ty_obj;
--drop TYPE Strsplit_type;
--drop TYPE ty_obj;
3. Test case: Implementing the Split Function function


 Create or Replace functionStrsplit (P_valuevarchar2, P_splitvarchar2:= ',')--Usage:select * FROM table (strsplit (' 1,2,3,4,5 ')) RETURNStrsplit_type pipelined isV_idxinteger; V_strvarchar2( -); V_strs_lastvarchar2(4000) :=P_value;beginLoop V_idx:=InStr (V_strs_last, P_split); Exit  whenV_idx= 0; V_STR:=SUBSTR (V_strs_last,1, V_idx- 1); V_strs_last:=substr (V_strs_last, V_idx+ 1); PipeRow (Ty_obj (V_STR)); EndLoop; PipeRow (Ty_obj (v_strs_last)); RETURN;EndStrsplit;





Oracle implements the Split function function


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.