ORACLE 表函數實現

來源:互聯網
上載者:User

標籤:

1、建立表物件類型。

在Oracle中想要返回表對象,必須自訂一個表類型,如下所示:

create or replace type t_table is table of number;

上面的類型定義好後,在function使用可用返回一列的表,如果需要多列的話,需要先定義一個物件類型。然後把物件類型替換上面語句中的number;

定義物件類型:

create or replace type obj_table as object
(
  id int,
  name varchar2(50)
)

修改表物件類型的定義語句如下:

create or replace type t_table is table of obj_table;

 

2、 建立示範函數

在函數的定義中,可以使用管道化表函數和普通的方式,下面提供兩種使用方式的代碼:

1)、管道化表函數方式:

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;

注意:管道的方式必須使用空的return表示結束.

調用函數的方式如下:

select * from table(f_pipe(5));

 

2)、 普通的方式:

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;

初始化值後還可以想注視行那樣進行修改.

調用方式如下:

select * from table(f_normal(5));

ORACLE 表函數實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.