【问题标题】:PLSQL Procedure to return result set without a refcursorPLSQL 过程返回没有引用的结果集
【发布时间】:2016-02-26 04:15:38
【问题描述】:
所以我正在学习 oracle 数据库课程,并且我有一个作业,我必须创建一个过程并在不使用 refcursor 的情况下“返回”(我猜是返回)结果集,但我发现的所有示例都使用了它。
假设我有这个:
CREATE OR REPLACE PROCEDURE get_all_inventory() AS
BEGIN
SELECT * FROM Inventory;
END;
/
如何在不使用 refcursor 的情况下使过程返回结果集?
这甚至可能吗?
谢谢。
编辑:如果有人知道以 json 格式返回结果集的方法,那就太棒了!
【问题讨论】:
标签:
oracle
stored-procedures
plsql
cursor
【解决方案1】:
除了使用 JSON,您还可以使用集合作为返回值。您必须首先为您的程序创建一个包。这是一个示例代码:
create OR REPLACE package get_all_inventory_package is
type arrayofrec is table of Inventory%rowtype index by pls_integer;
procedure get_all_inventory (o_return_variable OUT arrayofrec);
end;
/
create OR REPLACE package BODY get_all_inventory_package is
procedure get_all_inventory (o_return_variable OUT arrayofrec)is
return_variable arrayofrec;
begin
select * bulk collect into o_return_variable from Inventory;
end;
END;
/
declare
v_receiver get_all_inventory_package.arrayofrec;
begin
get_all_inventory_package.get_all_inventory(v_receiver);
for a in 1..v_receiver.count loop
dbms_output.put_line(v_receiver(a).Inventory_column);
end loop;
end;