Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

simple cursor runs infinite loop

Posted on 2014-12-23
5
Medium Priority
?
180 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
[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
  • 3
  • 2
5 Comments
 
LVL 59

Expert Comment

by:HainKurt
ID: 40515382
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 59

Expert Comment

by:HainKurt
ID: 40515384
problem was here:

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

Accepted Solution

by:
HainKurt earned 2000 total points
ID: 40515404
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
ID: 40515413
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
ID: 40515419
ok, that worked!  and makes sense, thank you HainKurt
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

730 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