Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 While loop value print

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-07-10 : 09:07:27
sac writes "*******************************************
declare @i int
declare @begin_hour1 varchar(200)
declare @begin_hour2 varchar(200)
declare @test varchar(200)

set @begin_hour1=6
set @begin_hour2=12

set @i=1
while @i<8
Begin
set @test='@begin_hour'+convert(varchar(20),@i)
print @test
set @i=@i+1

End
***************************************************

I want that in the loop the values of @begin_hour1......8 ,
@begin_hour1......12
should be printed.Whatever the value i assigned to the variables from @begin_hour1.........@begin_hour8 shoule be printed while appending @i to @begin_hour in a single while loop


Thanks in advance"

M.E.
Aged Yak Warrior

539 Posts

Posted - 2002-07-10 : 17:06:30
Well, first I gotta say that while loops like this aren't the best for sql logic. Better to stick to setbased queries.

That said, What you need to use here is dynamic sql (your close actually)

yours :
set @test='@begin_hour'+convert(varchar(20),@i)
print @test

try this:

set @test = 'print @begin_hour'+convert(varchar(20),@i)
exec(@test)

More then likely you won't need varchar(20) for a single digit like that.

-----------------------
Take my advice, I dare ya
Go to Top of Page
   

- Advertisement -