Solved

SImple curosr appears stuck in infinite loop.

Posted on 2014-12-07
2
139 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 70

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

820 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