gpt4 book ai didi

oracle - 是否强制关闭PLSQL block 中的游标

转载 作者:行者123 更新时间:2023-12-03 09:12:27 25 4
gpt4 key购买 nike

请参阅以下过程。我没有关闭光标,它工作正常。我在这里错过了什么吗?是强制关闭游标还是Oracle自动关闭游标?

CREATE OR REPLACE PROCEDURE pr_cursor_test IS
CURSOR emp_cur IS SELECT empno, ename FROM emp;
emprec emp_cur%rowtype;
BEGIN
OPEN emp_cur;
LOOP
FETCH emp_cur INTO emprec;
EXIT WHEN
emp_cur%notfound;
--Do Something
dbms_output.put_line(emprec.ename);
END LOOP;
END;

最佳答案

This article on Oracle website说的很清楚了。

When you declare a cursor in a package (that is, not inside a subprogram of the package) and the cursor is opened, it will stay open until you explicitly close it or your session is terminated.

When the cursor is declared in a declaration section (and not in a package), Oracle Database will also automatically close it when the block in which it is declared terminates. It is still, however, a good idea to explicitly close the cursor yourself. If the cursor is moved to a package, you will have the now necessary CLOSE already in place. And if it is local, then including a CLOSE statement will also show other developers and your manager that you are paying attention.

在您的代码示例中,CURSOR被声明为过程的一部分(而不是包的一部分),这意味着,当过程执行完成时,游标会自动关闭。但是,如果代码中有 OPENcursor 语句,最好的编码实践是 CLOSEcursor 语句。它使代码在将来易于理解和支持。

关于oracle - 是否强制关闭PLSQL block 中的游标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40903887/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com