2016-02-26 44 views
0

因此,我要通过一个oracle数据库课程,我有一个家庭作业,我必须创建一个过程和“返回”(我想返回)结果集,而不使用refcursor,但所有我发现的例子使用它。PLSQL过程返回结果集没有refcursor

所以可以说我有这样的:

CREATE OR REPLACE PROCEDURE get_all_inventory() AS 
BEGIN 
    SELECT * FROM Inventory; 
END; 
/

如何使过程中返回结果集,而无需使用REFCURSOR? 这甚至可能吗?

谢谢。

编辑:如果有人知道在json中返回结果集的方法,那将会非常棒!

+1

通过这个https://docs.oracle.com/cd/E17781_01/appdev.112/e18751/procedures_plsql.htm#TDPNG60040。你会得到一个细节想法 – Santhucool

+1

有可能返回为json。请参阅http://stackoverflow.com/questions/24006291/postgresql-return-result-set-as-json-array – Santhucool

回答

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;