Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

loop through records in stored procedure

Status
Not open for further replies.

BeanDog

Programmer
Jul 15, 2000
60
US
I have an open cursor, cur, and I want to simply loop through all the records and do some analysis. What is wrong with the following T-SQL taken from a stored procedure?

set @temp = 0
while (@temp IS NOT NULL)
BEGIN
FETCH cur INTO @temp
END

This loop never ends. Why not?

~BenDilts( void );
benbeandogdilts@cs.com
Long-time BASIC game programmer, Internet programmer and C++/DirectX of late.
 
Hi

The while loop condition should be

while (@@FETCH_STATUS=0)

@@FETCH_STATUS states whether has occurred correctly. This means that the code should look something like this

DECLARE lcur_name CURSOR FOR .....

OPEN lcur_name
FETCH NEXT FROM lcur_name

WHILE( @@FETCH_STATUS = 0 )
BEGIN
...More Code
FETCH NEXT FROM lcur_name
END

CLOSE lcur_name

DEALLOCATE lcur_name

Hope this is of some help


LokiDBA
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top