Avatar of Scarlett72
Scarlett72

asked on 

simple cursor runs infinite loop

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
Microsoft SQL ServerMicrosoft SQL Server 2008

Avatar of undefined
Last Comment
Scarlett72
Avatar of HainKurt
HainKurt
Flag of Canada image

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

Avatar of HainKurt
HainKurt
Flag of Canada image

problem was here:

select TenureID, MinMonths, MaxMonths, TenureName from #tTable;
>>>
select tID, MinValue, MaxValue, tName from #tTable;
ASKER CERTIFIED SOLUTION
Avatar of HainKurt
HainKurt
Flag of Canada image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Scarlett72
Scarlett72

ASKER

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 ...
Avatar of Scarlett72
Scarlett72

ASKER

ok, that worked!  and makes sense, thank you HainKurt
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo