For Loop in MSSQL

Hi Experts,

Is there a quick way that I can insert records in a MSSQL, using a for loop?

Example:

For I = 156 to 200

   INSERT INTO INVOICES (fldComment) Values ('abc');

Next I

Here, I'm trying to insert 44 identical records, for later use.

Any help appreciated


Thank you.
APD TorontoSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

nemws1Database AdministratorCommented:
Just us a WHILE loop, since it is functionally equivalent (there is no FOR loop in MSSQL)
DECLARE @start INT = 156;
DECLARE @end INT = 200;
DECLARE @loopvar INT;
SET @loopvar = @start;
WHILE (@loopvar <= @END)
BEGIN
    INSERT INTO INVOICES (fldComment) VALUES ('abc');
    SET @loopvar = @loopvar + 1;
END;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Harish VargheseProject LeaderCommented:
It is good to keep a table that stores a good number of serial numbers (say 1 to 30000) which will make the job easy when you want to generate statements like these. You can use below statement to create and generate such a table:
Create Table TableOfNumbers (Value int Primary Key)

;with cte as
(Select 1 as Num
	Union All
	Select num + 1
	From cte
	Where Num < 30000
)
Insert into TableofNumbers
Select * from cte 
option (maxrecursion 0)

Open in new window

And you can achieve the same result with a single SELECT statement as below:
INSERT INTO INVOICES (fldComment) 
SELECT 'abc'
FROM TableOfNumbers T
WHERE T.Value <= 44

Open in new window

0
HuaMin ChenProblem resolverCommented:
Here is the similar code for you.

declare @cnt int
set @cnt=1
WHILE @cnt<=40
BEGIN
   exit when @cnt>40

   insert into ...
   set @cnt=@cnt+1
END

Open in new window

0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Scott PletcherSenior DBACommented:
Thru SSMS (or any interface that supports the "GO" command), there's a very easy way:

GO
INSERT INTO INVOICES (fldComment) Values ('abc');
GO 44
0
APD TorontoSoftware DeveloperAuthor Commented:
That was easy :)
0
nemws1Database AdministratorCommented:
I must say, I do like ScottPletcher's solution.  I almost never use the numeric argument to GO and often forget it's there.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server 2008

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.