oracle存储过程(w/cursors)如何工作?

1 oracle stored-procedures cursors fetch

我有一个以下的oracle存储过程

    CREATE OR REPLACE
PROCEDURE getRejectedReasons
  (
    p_cursor IN OUT SYS_REFCURSOR)
AS
BEGIN
  OPEN p_cursor FOR SELECT * FROM reasons_for_rejection;
END;
Run Code Online (Sandbox Code Playgroud)

但是,当我在sql-developer中运行此存储过程时,我什么都看不到.我只是看到这样的事情:

Connecting to the database oracleLocal.
Process exited.
Disconnecting from the database oracleLocal.
Run Code Online (Sandbox Code Playgroud)

我是从MS sql server来的,我习惯在运行这样的存储过程时看到实际的结果.这个存储过程是不是返回结果因为我正在使用游标?

Dav*_*vid 5

存储过程正在返回一些东西,只是你没有对结果做任何事情.

您只需在SQLDeveloper中运行以下脚本即可完成此操作:


VARIABLE csr REFCURSOR;
EXEC getRejectedReasons(:csr); -- the colon identifies the parameter as a variable
PRINT csr;
Run Code Online (Sandbox Code Playgroud)

另一种方法是获取每一行并进行某种处理:


DECLARE
  -- sys_refcursor is weakly typed
  refcsr  SYS_REFCURSOR;
  -- define a record so we can reference the fields
  rej_rec Reasons_for_Rejection%ROWTYPE;
BEGIN

  getRejectedReasons(refcsr);

   -- loop through the results  
   LOOP
      -- gets one row at a time
      FETCH refcsr INTO rej_rec;
      -- if the fetch doesn't find any more rows exit the loop
      EXIT WHEN refcsr%NOTFOUND;
      -- Do something here.  
      -- For example : DBMS_OUTPUT.PUT_LINE(rej_rec.reason_desc);
    END LOOP;

END;
Run Code Online (Sandbox Code Playgroud)