0

调用存储过程的客户端只能处理记录集输出。

然而,由于几个内部查询,存储过程应该返回简单的 0 或 1 值。

有什么解决方法可以让存储过程变量值作为记录集返回吗?

4

3 回答 3

1

返回一个游标变量;使用 SELECT .. FROM DUAL 打开游标变量,其中包含要从过程中返回的值。

该过程将返回一个游标,该游标返回包含这些值的单行。

于 2013-01-11T18:29:17.723 回答
1

从你的程序做

OPEN resultsCursor_ FOR
Select 1 As aValue FROM DUAL;

或者

CREATE OR REPLACE PROCEDURE GetAValue
(
  results_       OUT     SYS_REFCURSOR
)
IS
    MY_COUNT_  INT;
BEGIN 

    MY_COUNT_ := 10;

    OPEN results_ FOR
    SELECT MY_COUNT_ AS MyCount FROM DUAL;

END GetAValue;
于 2013-01-11T18:30:10.333 回答
0

A procedure cannot return a value; you need a function to do this.

If you really want a procedure to "give" a cursor back to the caller, you can use something like this:

 --Declare this type in a package X
 --The caller must have access to the package X.
 TYPE ref_cursor IS REF CURSOR;
 CREATE OR REPLACE
 PROCEDURE test (
                   p_param1           IN  VARCHAR2,
                   p_cur              OUT X.REF_CURSOR,
                   p_error_code       OUT NUMBER,
                   p_error_message    OUT VARCHAR2
                 )
 AS
 BEGIN
      OPEN p_cur FOR
        SELECT * FROM TABLE;
 EXCEPTION
 WHEN OTHERS THEN

     p_error_code := SQLCODE;
     p_error_message := SQLERRM;

 END;

If there is no error set, the caller can then execute this cursor and fetch rows against it.

于 2013-01-11T18:56:51.947 回答