SQL服务器中的游标? [英] Cursor in SQL server?
问题描述
朋友们,
--OPEN CURSOR:
OPEN @getid
来自游标的FETCH行
FETCH NEXT
来自@getid INTO @id
WHILE @@ FETCH_STATUS
BEGIN
PRINT @id
FETCH NEXT
来自@getid INTO @id
结束
--CLOSE CURSOR
关闭@getid
- -DE-ALLOCATED CURSOR
DE-ALLOCATED @getid
i有点疑问,而@ @fetch_status
为什么我们在那里使用全局变量?任何一个澄清我的怀疑?
hi friends,
--OPEN CURSOR:
OPEN @getid
FETCH ROW FROM THE CURSOR
FETCH NEXT
FROM @getid INTO @id
WHILE @@FETCH_STATUS
BEGIN
PRINT @id
FETCH NEXT
FROM @getid INTO @id
END
--CLOSE CURSOR
CLOSE @getid
--DE-ALLOCATED CURSOR
DE-ALLOCATED @getid
i have doubt in while condition like while @@fetch_status
why we are using global variable there? any one clarify my doubt?
推荐答案
我不确定为什么它使用全局变量但这是我对这个主题的看法。
@@ FETCH_STATUS
对于连接上的所有游标都是全局的
http://msdn.microsoft.com/en-us/library/ms187308.aspx [ ^ ]
这意味着你必须要注意这种变量类型的行为。如果游标调用实现另一个游标的存储过程,则可能会产生问题,因为从存储过程返回时@@ FETCH_STATUS
值无效。诀窍是确保在执行提取后立即评估提取的结果。
但通常情况下,你可以做一些事情方法。您可以使用sys.dm_exec_cursors
管理功能检查任何给定游标的fetch_status
。
请阅读此处了解有关此功能的更多详情。
http://msdn.microsoft.com/en-us/library/ms190346.aspx [ ^ ]
I''m not sure as to why it uses a global variable but here are my thoughts on the subject.
"@@FETCH_STATUS
is global to all cursors on a connection"
http://msdn.microsoft.com/en-us/library/ms187308.aspx[^]
This means that you do have to be aware of the behaviour such variable type. You can create a problem if your cursor calls a stored procedure which implements another cursor as the@@FETCH_STATUS
value will be invalid when returning from the stored procedure. The trick is to make sure you always evaluate the result of a fetch immediately after executing the fetch.
But as is often the way, you can do things a number of ways. You can check thefetch_status
of any given cursor using thesys.dm_exec_cursors
management function.
Read here for more details on this function.
http://msdn.microsoft.com/en-us/library/ms190346.aspx[^]
这篇关于SQL服务器中的游标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!