Solved

SImple curosr appears stuck in infinite loop.

Posted on 2014-12-07
2
134 Views
Last Modified: 2014-12-07
SET NOCOUNT ON;

DECLARE @ssn char(9)
DECLARE @Code char(3)
DECLARE @DateEntered char(9)


Hello, I have a simple table with three columns. The table only has like 6 records in it at this time. All I want to do is to read the contents of the table into a cursor, and then dump out the data. I am running the below script from query analyzer, but it appears to be stuck in an infinite loop. Can someone look at my cursor code, and tell me if it is broken?


PRINT '--------  Report --------';

DECLARE people_cursor CURSOR FOR
SELECT SSN, Code,DateEntered
FROM People
ORDER BY DateEntered;

OPEN people_cursor

FETCH NEXT FROM people_cursor
INTO @ssn, @Code, @DateEntered

WHILE @@FETCH_STATUS = 0
BEGIN

      PRINT @ssn
      PRINT @Code
      PRINT @DateEntered

    CLOSE people_cursor
    DEALLOCATE people_cursor

END
0
Comment
Question by:brgdotnet
2 Comments
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 40485539
I see at least 2 issues:
-close/deallocate goes outside the loop
-you are not refetching the next row in the loop

DECLARE people_cursor CURSOR FOR 
SELECT SSN, Code,DateEntered
FROM People
ORDER BY DateEntered;

OPEN people_cursor

FETCH NEXT FROM people_cursor 
INTO @ssn, @Code, @DateEntered

WHILE @@FETCH_STATUS = 0
BEGIN

      PRINT @ssn
      PRINT @Code
      PRINT @DateEntered

     FETCH NEXT FROM people_cursor 
     INTO @ssn, @Code, @DateEntered

END

    CLOSE people_cursor
    DEALLOCATE people_cursor

Open in new window

0
 
LVL 2

Author Closing Comment

by:brgdotnet
ID: 40485551
Thank you Eric,

I am just now learning about cursors.
0

Featured Post

Get up to 2TB FREE CLOUD per backup license!

An exclusive Black Friday offer just for Expert Exchange audience! Buy any of our top-rated backup solutions & get up to 2TB free cloud per system! Perform local & cloud backup in the same step, and restore instantly—anytime, anywhere. Grab this deal now before it disappears!

Join & Write a Comment

Suggested Solutions

Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now