• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

SQL Stored Procedure Loop

lo5750ul

Senior member
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.
 

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
 
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.
 
Back
Top