返回游标,从其他过程调用过程

时间:2013-09-13 16:10:06

标签: sql database oracle stored-procedures sys-refcursor

我在同一个包中有2个程序。 我希望在QOT_LST_M_QOT_PRE中使用QOT_LST_M_SEC_EXC_PRE。在QOT_LST_M_SEC_EXC_PR E中我希望找到参数 - x_qot_id,使用此参数调用QOT_LST_M_QOT_PRE并返回它而不是语句。我可以做吗?怎么样。 我的意思是

    PROCEDURE QOT_LST_M_SEC_EXC_PRE (
        i_sec_id     IN NUMBER,
        i_exc_id     IN NUMBER,
        o_recordset  OUT SYS_REFCURSOR ) IS x_qot_id NUMBER(10); 
        ------------------------------   
    BEGIN

    ---------------------------------------------------------------

    --call a function instead of writing query from this function
    open o_recordset for QOT_LST_M_QOT_PRE(x_qot_id, o_recordset);
    ----------------------------------------------------------------

    END  QOT_LST_M_SEC_EXC_PRE;





     PROCEDURE QOT_LST_M_QOT_PRE
    (
        i_qot_id     IN NUMBER,            
        o_recordset  OUT SYS_REFCURSOR
        --------------------------------
    );

1 个答案:

答案 0 :(得分:0)

当然可以。您可以声明类型SYS_REFCURSOR的参数并在您的过程中使用它们,这是一个示例:

CREATE OR REPLACE PROCEDURE QOT_LST_M_QOT_PRE (i_qot_id IN NUMBER, THE_CURSOR OUT SYS_REFCURSOR) --Declare a sys_refcursor to be an out parameter, so it can be used outside
AS
BEGIN
 OPEN THE_CURSOR FOR SELECT * FROM THE_TABLE WHERE X=i_qot_id;--Open the cursor
END;

CREATE OR REPLACE PROCEDURE QOT_LST_M_SEC_EXC_PRE (i_sec_id IN NUMBER, i_exc_id IN NUMBER)
AS
x_qot_id NUMBER(10); --Test param
RESULT_CURSOR SYS_REFCURSOR;--The cursor that will hold the opened cursor in QOT_LST_M_QOT_PRE procedure
SOMEVARIABLE VARCHAR2(10);--Test variables to hold results of cursor
BEGIN
 QOT_LST_M_QOT_PRE(1,RESULT_CURSOR);--Procedure will open RESULT_CURSOR
 LOOP --Loop cursor
    FETCH RESULT_CURSOR INTO SOMEVARIABLE;
    EXIT WHEN RESULT_CURSOR%NOTFOUND;
    DBMS_OUTPUT.PUT_LINE('READ :'||SOMEVARIABLE);
 END LOOP;
 CLOSE RESULT_CURSOR;--Close the opened cursor
END;