在 Oracle 11g 中,当 PL/SQL 上下文整理函数/过程自动关闭打开的游标时。为什么在网络用户打开和关闭光标的许多示例中?
这是向后兼容吗?
REF-CURSOR 呢?离开程序也关闭它们?
如果游标应该总是关闭,那么处理异常呢?在EXCEPTION
块中我必须检查所有游标ISOPEN
然后关闭它们吗?
基本脚本演示自动关闭功能:
DECLARE
PROCEDURE TEST IS
CURSOR CUR_CLIENTS IS SELECT DUMMY CL_ID FROM DUAL;
TYPE RT_CLIENTS IS TABLE OF CUR_CLIENTS%ROWTYPE;
LT_CLIENTS RT_CLIENTS;
BEGIN
IF CUR_CLIENTS%ISOPEN THEN
DBMS_OUTPUT.PUT_LINE('CLOSING CURSOR');
CLOSE CUR_CLIENTS;
END IF;
OPEN CUR_CLIENTS;
LOOP
FETCH CUR_CLIENTS BULK COLLECT INTO LT_CLIENTS LIMIT 1000;
EXIT WHEN LT_CLIENTS.COUNT = 0;
FOR I IN 1..LT_CLIENTS.COUNT LOOP
DBMS_OUTPUT.PUT_LINE(LT_CLIENTS(I).CL_ID);
END LOOP;
END LOOP;
END TEST;
BEGIN
DBMS_OUTPUT.PUT_LINE('--------------------');
TEST;
DBMS_OUTPUT.PUT_LINE('--------------------');
TEST;
DBMS_OUTPUT.PUT_LINE('--------------------');
TEST;
DBMS_OUTPUT.PUT_LINE('--------------------');
END;