Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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?
Posted
Comments
StackQ 14-Dec-12 7:11am    
gud question.I also want to know?

1 solution

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 the fetch_status of any given cursor using the sys.dm_exec_cursors management function.

Read here for more details on this function.

http://msdn.microsoft.com/en-us/library/ms190346.aspx[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900