2016-02-26 92 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;