Introduction to the Table set returned by Oracle Functions

Source: Internet
Author: User

When calling the Oracle function, in order for the PL/SQL function to return multiple rows of data, a REF CURSOR or a data set must be returned. Ref cursor is limited to the data that can be selected from the query. The entire set must be specific before it can be returned. 9i corrected the latter situation by introducing the pipeline table function in Oracle functions. A table function is a function that returns a set of the entire row. It can be queried directly from an SQL statement, just as if it is a real database table. Pipeline table functions are similar, but they return data as they are during build, rather than all at once. Pipeline table functions are more effective because data can be returned as quickly as possible.

The canonicalized table function must return a set. In a function, the pipe row statement is used to RETURN a single element of the set. The function must end with an empty RETURN statement to indicate that it has been completed. Once we create the above function, we can use the TABLE operator to call it from the SQL query, so that the Oracle function returns the Table set.

1. Use a custom type

 
 
  1. /* Formatted on 2010/02/26 08:42 (Formatter Plus v4.8.8) */  
  2. CREATE OR REPLACE TYPE objemp AS OBJECT (  
  3.    maxsal   NUMBER,  
  4.    minsal   NUMBER  
  5. );  
  6. /* Formatted on 2010/02/26 08:43 (Formatter Plus v4.8.8) */  
  7. CREATE OR REPLACE TYPE tabemp AS TABLE OF objemp;  
  8.  
  9.    

2. Use the Pipeline Function and Pipe row ()

 
 
  1. CREATE OR REPLACE FUNCTION getmaxminsalary (department NUMBER)  
  2.    RETURN tabemp PIPELINED  
  3. AS  
  4.    maximum_salary   NUMBER;  
  5.    minimum_salary   NUMBER;  
  6.    v_errorcode      NUMBER;  
  7.    v_errortext      VARCHAR2 (200);  
  8.    v                objemp;  
  9. BEGIN  
  10.    FOR myrow IN (SELECT MAX (sal) m_sal, MIN (sal) min_sal  
  11.                    FROM emp  
  12.                   WHERE deptno = departmnet)  
  13.    LOOP  
  14.       v := (myrow.m_sal, myrow.min_sal);  
  15.       PIPE ROW (v);  
  16.    END LOOP;  
  17.    RETURN;  
  18. EXCEPTION  
  19.    WHEN OTHERS  
  20.    THEN  
  21.       v_errorcode := SQLCODE;  
  22.       v_errortext := SUBSTR (SQLERRM, 1, 200);  
  23.       INSERT INTO log_table  
  24.                   (code, MESSAGE, info  
  25.                   )  
  26.            VALUES (v_errorcode, v_errortext, 'getMaxMinSalary'  
  27.                  );  
  28. END;  
  29.  

3. Use the Table Operator

 
 
  1. SELECT * FROM TABLE(getMaxMinSalary(10)); 

The above is an example of a good Oracle function that returns the Table set. If you are interested, try it.

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.