Three ways to return an Oracle object table using a function _oracle

Source: Internet
Author: User

Because you want to return a Table object, you declare the global type first, regardless of what method is followed, and the field variable type is object and cannot be a record:

Create or replace type T_test as Object (
 EMPNO number  (4),
 ename  VARCHAR2,
 JOB   VARCHAR2 (9 ),
 SAL number   (7,2)
);

Create or replace type t_test_table as table of t_test;

As for the method of returning the Table object, three kinds are found:
1, with the array

Create or Replace function F_test_array (v_deptno in number default null) return
t_test_table
is 
v_test t_ Test_table: = T_test_table ();
Cursor cur is select empno, ename, Job, Sal from emp where deptno = V_deptno;
Begin for
C in cur loop
v_test.extend ();
V_test (v_test.count): = T_test (C.empno, C.ename, C.job, c.sal);
End Loop;
return v_test;
End

2, with pipe

Create or Replace function F_test_pipe (v_deptno in number default null) return
t_test_table pipelined 
is
v_ Test t_test_table: = T_test_table ();
Cursor cur is select empno, ename, Job, Sal from emp where deptno = V_deptno;
Begin for 
C in cur loop
pipe row (t_test (C.empno, C.ename, C.job, c.sal)); 
End Loop; 
return; 
End

Both of these need to be traversed by a cursor to get the Table object, the performance of the estimated 2nd higher.

3, with collect (do not need cursors, the code is relatively simple)

Create or Replace function F_test_collect (v_deptno in number default null) return
t_test_table
is 
v_test T_test_table: = T_test_table ();
Begin
Select T_test (empno, ename, Job, sal) bulk collect into V_test from emp where deptno = V_deptno;
return v_test;
End

It should be noted that the output is instantiated before select INTO:
t_test (empno, ename, Job, Sal)
Otherwise there will be an error:
ORA-00947: Not enough values (object multiple fields)
ORA-00932: inconsistent data type (object single field)
And if the type is declare directly in the Plsql block, it is not necessary to first object the output result.
Three function definitions can test the output the same way:

SELECT * FROM table (f_test_pipe);
SELECT * FROM table (F_test_array);
SELECT * FROM table (F_test_collect (30));

The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.

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.