Solved

simple cursor runs infinite loop

Posted on 2014-12-23
5
154 Views
Last Modified: 2014-12-23
Hi, I'm trying to build a simple cursor to understand how they work. From the temp table, I would like to print out the values of the table, when I run my cursor it just keeps running the output of the first row infinitely. I just want it to print out the 7 rows in the table ...

IF OBJECT_ID('TempDB..#tTable','U') IS NOT NULL
         DROP TABLE #tTable

 CREATE TABLE #tTable
        (
        tID int,
        minValue int,
        maxValue int,
        tName varchar(25)        
        )
        insert into #tTable
        (tID, MinValue, MaxValue, tName)
SELECT '1','0','3','0-3 Mths' UNION ALL
SELECT '2','3','6','3-6 Mths' UNION ALL
SELECT '3','6','9','6-9 Mths' UNION ALL
SELECT '4','9','12','9-12 Mths' UNION ALL
SELECT '5','12','18','12-18 Mths' UNION ALL
SELECT '6','18','24','18-24 Mths' UNION ALL
SELECT '7','24','9999','24+ Mths'        
       
select * from #tTable

declare @tid as int;
declare @min as int;
declare @max as int;
declare @tn as varchar(25);

declare @otCursor as cursor;

set @otCursor = cursor for
select TenureID, MinMonths, MaxMonths, TenureName from #tTable;

open @otCursor;
fetch next from @otCursor into @tid,@min,@max,@tn
while @@fetch_status = 0
begin
      print
      cast(@tid as varchar(50)) + ' ' +
      cast(@min as varchar(50)) + ' ' + cast(@max as varchar(50)) + ' ' +
      @tn;
end

close @otCursor
deallocate @otCursor
0
Comment
Question by:Scarlett72
  • 3
  • 2
5 Comments
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
here:

 CREATE TABLE #tTable 
        (
        tID int,
        minValue int,
        maxValue int,
        tName varchar(25)        
        )
        insert into #tTable
        (tID, MinValue, MaxValue, tName)
SELECT '1','0','3','0-3 Mths' UNION ALL
SELECT '2','3','6','3-6 Mths' UNION ALL
SELECT '3','6','9','6-9 Mths' UNION ALL
SELECT '4','9','12','9-12 Mths' UNION ALL
SELECT '5','12','18','12-18 Mths' UNION ALL
SELECT '6','18','24','18-24 Mths' UNION ALL
SELECT '7','24','9999','24+ Mths'        
        
select * from #tTable

declare @tid as int;
declare @min as int;
declare @max as int;
declare @tn as varchar(25);

declare @otCursor as cursor;

set @otCursor = cursor for
select tID, MinValue, MaxValue, tName from #tTable;

open @otCursor;
fetch next from @otCursor into @tid,@min,@max,@tn
while @@fetch_status = 0
begin
      print
      cast(@tid as varchar(50)) + ' ' +
      cast(@min as varchar(50)) + ' ' + cast(@max as varchar(50)) + ' ' +
      @tn;
end

close @otCursor
deallocate @otCursor

Open in new window

0
 
LVL 51

Expert Comment

by:HainKurt
Comment Utility
problem was here:

select TenureID, MinMonths, MaxMonths, TenureName from #tTable;
>>>
select tID, MinValue, MaxValue, tName from #tTable;
0
 
LVL 51

Accepted Solution

by:
HainKurt earned 500 total points
Comment Utility
oops, another issue :) you need another fetch in loop, here it is:

IF OBJECT_ID('TempDB..#tTable','U') IS NOT NULL
         DROP TABLE #tTable

 CREATE TABLE #tTable 
        (
        tID int,
        minValue int,
        maxValue int,
        tName varchar(25)        
        )
        insert into #tTable
        (tID, MinValue, MaxValue, tName)
SELECT '1','0','3','0-3 Mths' UNION ALL
SELECT '2','3','6','3-6 Mths' UNION ALL
SELECT '3','6','9','6-9 Mths' UNION ALL
SELECT '4','9','12','9-12 Mths' UNION ALL
SELECT '5','12','18','12-18 Mths' UNION ALL
SELECT '6','18','24','18-24 Mths' UNION ALL
SELECT '7','24','9999','24+ Mths'        
        
select * from #tTable

declare @tid as int;
declare @min as int;
declare @max as int;
declare @tn as varchar(25);

declare @otCursor as cursor;

set @otCursor = cursor for
select tID, MinValue, MaxValue, tName from #tTable;

open @otCursor;
fetch next from @otCursor into @tid,@min,@max,@tn
while @@fetch_status = 0
begin
      print
      cast(@tid as varchar(50)) + ' ' +
      cast(@min as varchar(50)) + ' ' + cast(@max as varchar(50)) + ' ' + @tn;
	  fetch next from @otCursor into @tid,@min,@max,@tn
end

close @otCursor
deallocate @otCursor

Open in new window

0
 

Author Comment

by:Scarlett72
Comment Utility
Hi HainKurt, thank you for replying, the samething is happening for me, it just keeps running '1 0 3 0-3 Mths'
over and over again ... it must be something simple ...
0
 

Author Comment

by:Scarlett72
Comment Utility
ok, that worked!  and makes sense, thank you HainKurt
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

772 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

10 Experts available now in Live!

Get 1:1 Help Now