如何在程序中返回数据集?

时间:2017-04-18 06:17:30

标签: oracle stored-procedures plsql oracle11gr2 sys-refcursor

我有一个这样的程序:

CREATE OR REPLACE PROCEDURE RIC.TEST 
(P_VAR1 IN VARCHAR2, P_VAR2 IN VARCHAR2, P_VAR3 IN VARCHAR2, P_VAR4 IN VARCHAR2)
IS 
L_RC SYS_REFCURSOR;
BEGIN
INSERT INTO RIC.TEMP_TABLE
    SELECT * FROM RIC.TABLE WHERE COL1=P_VAR1 AND COL2=P_VAR2 AND COL3=P_VAR3 AND COL4=P_VAR4 AND ...;

OPEN L_RC
FOR 'SELECT .... FROM RIC.TEMP_TABLE WHERE ...';
...

TEMP_TABLE是一个临时表。当我运行此过程时,我需要返回并显示第二个选择输出(SELECT .... FROM RIC.TEMP_TABLE WHERE ...)。我怎样才能做到这一点?光标错误的方法是什么?

谢谢。

1 个答案:

答案 0 :(得分:2)

将程序修改为

CREATE OR REPLACE PROCEDURE ric.test (p_var1     IN     VARCHAR2,
                                      p_var2     IN     VARCHAR2,
                                      p_var3     IN     VARCHAR2,
                                      p_var4     IN     VARCHAR2,
                                      p_cursor      OUT SYS_REFCURSOR)
IS
BEGIN
   INSERT INTO ric.temp_table
      SELECT *
        FROM ric.table
       WHERE col1 = p_var1 AND col2 = p_var2 AND col3 = p_var3;

   OPEN p_cursor FOR SELECT * FROM ric.temp_table;
END;
/

并执行

程序
VARIABLE cur REFCURSOR;
EXEC test (:cur);
PRINT cur;

如果您想使用SQL执行该过程,请将该过程包装在一个函数中,然后在SQL语句中调用该函数。 Example