SQL Stored Procedure Loop

lo5750ul

Senior member
Jul 18, 2001
744
0
76
Hey all,

I would like to do a simple counter loop in an SQL Stored Procedure. I am passing a variable in to the SP and I want to count from 1 to the variable.

Does anyone know how to do it?

Cheers.
 

SQL

Member
Jul 10, 2001
115
0
0

Several ways depending on which database.

Stripped down SQL Server:

declare @count int

select @count=1

while(@count<=@var1)
begin
print @count -- or select @count works as well
select @count=@count+1
end
 

lo5750ul

Senior member
Jul 18, 2001
744
0
76
SQL,

Thanks for the reply. I had worked that bit out. The next question is, how do you take the counter and create a variable name with it.

e.g. DECALRE @strInput + count to end up as @strInput1, @strInput 2 etc.