Solved

SImple curosr appears stuck in infinite loop.

Posted on 2014-12-07
2
142 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
CAST issue with SQL 2000 15 42
Any benefit to adding a Clustered index here? 4 35
Failover Cluster Primary Nodes Current Vote = 0 5 30
SQL query 45 33
Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
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.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

739 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