我正在尝试找到一种简单的方法来使用Oracle的表值函数,因为我对此很陌生。
我在网上找到了有效的教程。但是,与表值函数的其他非Oracle实现相比,它相当复杂。
在本教程中,它会执行以下操作:
create or replace type t_record as object (
i number,
n varchar2(30)
);
然后创建表
create or replace type t_table as table of t_record;
最后创建函数
create or replace function return_table return t_table as
v_ret t_table;
begin
--
-- Call constructor to create the returned
-- variable:
--
v_ret := t_table();
--
-- Add one record after another to the returned table.
-- Note: the »table« must be extended before adding
-- another record:
--
v_ret.extend; v_ret(v_ret.count) := t_record(1, 'one' );
v_ret.extend; v_ret(v_ret.count) := t_record(2, 'two' );
v_ret.extend; v_ret(v_ret.count) := t_record(3, 'three');
--
-- Return the record:
--
return v_ret;
end return_table;
/
然后我们可以使用查询调用简单地获取函数的结果。
select * from table(return_table);
有没有更简单,更简洁的方法来执行此操作,就像仅将查询编写为函数的主体并返回它,而无需进行所有的初始化,扩展和添加?。
非常感谢。
或者,您可以创建一个表:
SQL> create table tab(
i number,
n varchar2(30)
);
并填充:
SQL> insert all
into tab values(1, 'one')
into tab values(2, 'two')
into tab values(3, 'three')
select * from dual;
然后通过函数中的隐式游标填充类型对象值:
SQL> create or replace function return_table return t_table as
v_ret t_table:= t_table();
begin
for c in ( select * from tab )
loop
v_ret.extend; v_ret(v_ret.count) := t_record( c.i, c.n );
end loop;
return v_ret;
end return_table;
/
并致电:
SQL> select * from table(return_table);
I N
- -----
1 One
2 Two
3 Three