游标打开后不关闭会有什么问题

firein 2008-08-22 04:22:44
比如现在程序里调用一个存储过程,存储过程里使用并打开了游标,执行到最后没有关闭游标。

但是程序里会关闭事务和连接。这样的话不关闭游标会有什么问题吗?
...全文
2186 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
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<session or connection)

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]
啊哦 这样的话 不关闭游标也没大碍嘛~!~~
  • 打赏
  • 举报
回复
断开此次连接后,游标就不存在了,但是在断开连接之前,会提示游标已经存在或者打开
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
  • 打赏
  • 举报
回复
回复看回复

34,838

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧