您需要做的就是在完成后在光标上发出一个 CLOSE ,无论它实际上是在哪里打开的:
-- A function to return a SYS_REFCURSOR
CREATE OR REPLACE FUNCTION f_c RETURN SYS_REFCURSOR IS
cur SYS_REFCURSOR;
BEGIN
OPEN cur FOR SELECT LEVEL FROM dual CONNECT BY LEVEL < 10;
RETURN cur;
END;
这是一个示例运行:
DECLARE
cc SYS_REFCURSOR;
r VARCHAR2(10);
BEGIN
cc := f_c; -- Get the cursor from the function
LOOP
FETCH cc INTO r;
EXIT WHEN cc%NOTFOUND;
dbms_output.put_line('Output is: '||r);
END LOOP;
CLOSE cc; -- Close the SYS_REFCURSOR returned from the function
END;
/
Output is: 1
Output is: 2
Output is: 3
Output is: 4
Output is: 5
Output is: 6
Output is: 7
Output is: 8
Output is: 9
至于从函数或过程返回一组值,这是关于该主题的另一个SO question 。