ORACLE table Function Implementation

Source: Internet
Author: User

1. Create a Table object type.

To return a Table object in Oracle, you must customize a table type as follows:

Create or Replace type t_table is table of number;

When the above type is defined, the function uses a table that can return a column, and if more than one column is required, you need to define an object type first. Then replace the object type with the number in the above statement;

Define the object type:

Create or replace type Obj_table as Object
(
ID int,
Name VARCHAR2 (50)
)

The definition statement that modifies the Table object type is as follows:

Create or Replace type t_table is table of obj_table;

2. Create a demo function

In the definition of a function, you can use the Pipelined table function and the normal way, the following provides two ways to use the code:

1), pipelined table function mode:

Create or Replace function F_pipe (s number)
return t_table pipelined
As
V_obj_table obj_table;
Begin
For I in 1..s loop
V_obj_table: = Obj_table (I,to_char (i*i));
Pipe row (v_obj_table);
End Loop;
Return
End F_pipe;

Note: The way the pipeline must end with an empty return.

The function is called in the following way:

SELECT * FROM Table (F_pipe (5));

2), The ordinary way:

Create or Replace function F_normal (s number)
Return t_table
As
RS t_table:= t_table ();
Begin
For I in 1..s loop
Rs.extend;
RS (rs.count): = Obj_table (rs.count, ' name ' | | To_char (Rs.count));
--rs (Rs.count). Name: = RS (rs.count). Name | | ' XXXX ';
End Loop;
Return RS;
End F_normal;

After initializing the value, you can modify it as you would like to gaze at the line.

The method is called as follows:

SELECT * FROM Table (F_normal (5));

ORACLE table Function Implementation

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.