For Loop in SQL Server 2000
August 24th 2007Here’s a just-so-crazy-it-might-work way of doing a for loop in sql server.
This comes in real handy when making fake data for testing purposes.
DECLARE @count INT DECLARE @command_to_loop NVARCHAR(1000) -- This example runs from 1 to 10 SET @count = 1 WHILE(@count <= 10) BEGIN -- This is just my crazy example -- Replace @command_to_loop with whatever you want to execute loop SET @command_to_loop = 'insert into customers (name) values (''Billy Bob' + cast(@count as varchar(3)) + ''')' print @command_to_loop EXEC sp_executesql @command_to_loop SET @count = @count + 1 END