显式打开和关闭游标 [英] Explicit Opening and Closing cursors
问题描述
我一直在阅读数据库游标,而我所看到的示例代码的每一位都明确地打开和关闭了游标.我只使用过几次,所以从来没有这样做.谁能告诉我为什么有必要这样做吗?我知道,如果您不关闭游标,则会造成内存泄漏,但我从未必须打开过.
I've been reading up on database cursors, and every bit of sample code I've seen explicitly opens and closes the cursor. I've only used them a few times and I've never had to do this. Can anyone tell me why it is necessary to do this? I know if you don't close a cursor you can create memory leakes but i've never had to open one.
谢谢
推荐答案
如果您的游标是全局游标(我猜您在没有问题的地方使用了本地游标,因为它们超出范围时会关闭),则必须明确关闭它们.
If your cursors are global(I guess you used local cursors where this is not a problem, because they are closed when they get out of scope) you must explicitly close them.
有关不关闭全局游标的几个问题
There are several problems about not closing global cursors
- 用于游标的内存未释放
- 您可以打开的游标数量已达到上限.最终,您将无法创建另一个游标,因为您将打开最大数量的游标.
如果您要问为什么我必须使用游标? -有时您需要遍历各行(例如使用常规的for循环).您不能在面向集合的方法中做到这一点-必须使用游标.
If you are asking why do I have to use cursors? - Sometimes you need to loop through the rows (like using a regular for loop). You can not do that in the set oriented approach - you have to use cursors.
这篇关于显式打开和关闭游标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!