[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

For Loop in MSSQL

Posted on 2014-08-02
6
Medium Priority
?
286 Views
Last Modified: 2014-08-03
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.
0
Comment
Question by:APD_Toronto
6 Comments
 
LVL 23

Accepted Solution

by:
nemws1 earned 2000 total points
ID: 40236887
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
 
LVL 12

Expert Comment

by:Harish Varghese
ID: 40237057
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
 
LVL 11

Expert Comment

by:HuaMinChen
ID: 40237064
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 70

Expert Comment

by:Scott Pletcher
ID: 40237591
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
 

Author Closing Comment

by:APD_Toronto
ID: 40238016
That was easy :)
0
 
LVL 23

Expert Comment

by:nemws1
ID: 40238030
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question