社区

MS-SQL Server 帖子详情 游标打开后不关闭会有什么问题 firein 2008-08-22 04:22:44 比如现在程序里调用一个存储过程,存储过程里使用并打开了游标,执行到最后没有关闭游标。

但是程序里会关闭事务和连接。这样的话不关闭游标会有什么问题吗?

...全文

2190 14 打赏 收藏 游标打开后不关闭会有什么问题 比如现在程序里调用一个存储过程,存储过程里使用并打开了游标,执行到最后没有关闭游标。 但是程序里会关闭事务和连接。这样的话不关闭游标会有什么问题吗? 复制链接

扫一扫 分享 转发到动态 举报 AI 作业

写回复 配置赞助广告取 消

确 定

用AI写文章 14 条回复 切换为时间正序 请发表友善的回复… 发表回复 打赏红包 需支付: 0.00 元 取 消 确 定 tianhuo_soft 2008-08-23 打赏举报 回复

CREATE PROCEDURE [dbo].[pro_CURSOR]

AS

BEGIN

--声明一个游标

DECLARE MyCURSOR CURSOR FOR

SELECT login_date FROM table

--打开游标

open MyCURSOR

close MyCURSOR

deallocate MyCURSOR

return 0

end

regithanhu 2008-08-23 打赏举报 回复 传说没有影响! zbc1009 2008-08-22 打赏举报 回复 从MSDN摘抄,然后用很憋的水平翻译了一下。。。

LOCAL cursor

Specifies that the scope of the cursor is local to the batch, stored procedure, or trigger in which the cursor was created. The cursor name is only valid within this scope. The cursor can be referenced by local cursor variables in the batch, stored procedure, or trigger, or a stored procedure OUTPUT parameter. An OUTPUT parameter is used to pass the local cursor back to the calling batch, stored procedure, or trigger, which can assign the parameter to a cursor variable to reference the cursor after the stored procedure terminates. The cursor is implicitly deallocated when the batch, stored procedure, or trigger terminates, unless the cursor was passed back in an OUTPUT parameter. If it is passed back in an OUTPUT parameter, the cursor is deallocated when the last variable referencing it is deallocated or goes out of scope.

作用域为一个scope。cursor仅在当前scope有效。可以被当前scope内的局部cursor变量引用,也可以作为SP的输出参数。作为输出参数时,就是把该cursor返回给调用着。cursor在当前scope结束时隐式的清除,当作为SP输出参数时有点特殊,是当引用它的变量清除时清除,或者调用的scope结束时清除。(scope是指一个batch,或SP,或trigger,scope

GLOBAL cursor

Specifies that the scope of the cursor is global to the connection. The cursor name can be referenced in any stored procedure or batch executed by the connection. The cursor is only implicitly deallocated at disconnect.

作用域是connection。可以被当前connection内的所有scope访问。当连接结束时候清除。

总之,connection结束时候肯定已经清除了cursor.

cxmcxm 2008-08-22 打赏举报 回复 如果连接已关闭,就没事。

最好手动关闭并释放,养成编程好习惯 ybkenan 2008-08-22 打赏举报 回复 沒事的。 firein 2008-08-22 打赏举报 回复 [Quote=引用 6 楼 marco08 的回复:]

关闭连接呢?

--

下次调用SP时一样有提示吧

[/Quote]

怎么说法不一样 firein 2008-08-22 打赏举报 回复 [Quote=引用 7 楼 HEROWANG 的回复:]

断开此次连接后,游标就不存在了,但是在断开连接之前,会提示游标已经存在或者打开

[/Quote]

啊哦 这样的话 不关闭游标也没大碍嘛~!~~ 让你望见影子的墙 2008-08-22 打赏举报 回复 断开此次连接后,游标就不存在了,但是在断开连接之前,会提示游标已经存在或者打开 marco08 2008-08-22 打赏举报 回复 关闭连接呢?

--

下次调用SP时一样有提示吧 firein 2008-08-22 打赏举报 回复 [Quote=引用 3 楼 tianhuo_soft 的回复:]

关闭事务 就关闭了 游标

但是建议自己手动关闭

不要希望windwos为我们做太多的事情

[/Quote]

关闭连接呢? kenryu3450 2008-08-22 打赏举报 回复 如果在服务器端执行,下次引用的时候会报错:提示游标已经open tianhuo_soft 2008-08-22 打赏举报 回复 关闭事务 就关闭了 游标

但是建议自己手动关闭

不要希望windwos为我们做太多的事情 zoujp_xyz 2008-08-22 打赏举报 回复 释放资源 firein 2008-08-22 打赏举报 回复 回复看回复 Oracle存储过程游标详解 Oracle存储过程游标详解,针对Oracle存储过程游标详细说明。 Oracle游标使用案例大全 该资源系本人培训期间的关于ORCL 游标所有实用案例,在此共享希望对大家学习有所帮助 SQL游标原理和使用方法 SQL游标原理和使用方法,适合新老手使用 Oracle的游标学习 NULL

博文链接:https://longhuang.iteye.com/blog/694275 快速掌握Oracle数据库游标的使用方法 当查询返回结果超过一行时,就需要一个显式游标,此时用户不能使用select into语句。本文介绍了Oracle数据库游标的使用方法。

MS-SQL Server

34,837

社区成员

254,632

社区内容

发帖 与我相关 我的任务 MS-SQL Server MS-SQL Server相关内容讨论专区 复制链接

扫一扫 分享 确定 社区描述 MS-SQL Server相关内容讨论专区 社区管理员

加入社区

获取链接或二维码

近7日

近30日

至今

加载中

查看更多榜单

社区公告

暂无公告 试试用AI创作助手写篇文章吧

+ 用AI写文章