【发布时间】:2019-12-20 19:27:39
【问题描述】:
我正在尝试找到一种简单的方法来使用 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);
有没有更简单、更简洁的方法来做到这一点,比如只将查询编写为函数的主体并返回它而不进行所有初始化、扩展和添加?。
非常感谢。
【问题讨论】:
-
如果您不想像表一样使用它(例如在 JOIN 或添加 WHERE 子句中),那么您可以使用过程并通过
DBMS_SQL.RETURN_RESULT返回结果 -
不幸的是 PL/SQL 没有匿名类型 - 你不能有
function f return table of t%rowtype,你必须指定一个现有的类型。但是,一旦定义了它,您就可以在任何地方重复使用它。但是你确定你需要一个表函数吗? -
@WilliamRobertson,我认为在我的情况下,视图可以解决问题。感谢您的回答
标签: oracle plsql stored-functions