loads of useful information, examples and tutorials pertaining to web development utilizing asp.net, c#, vb, css, xhtml, javascript, sql, xml, ajax and everything else...

 



Advertise Here
C-Sharpener.com - Programming is Easy!  Learn Asp.Net & C# in just days, Guaranteed!

Running a 'For' Loop in MS SQL

by naspinski 9/10/2008 7:28:00 AM

There really isn't a 'for' loop in MS SQL, but you can do basically the same thing

First of all, I am far from a SQL master; I would even go as far to say I would be lucky to be an amatuer at best.  But I did stumble upon a convenient way for populating a lot of new rows using a simple technique that is not using a bulk insert.  It is actually pretty basic stuff, but nice to know if you were Googling for it like I was.

 

DECLARE @count INT

SET @count = 0

WHILE (@count < 40)

BEGIN

   INSERT INTO some_table ([columnA], [columnB]) VALUES ('val1', 'val2')

   SET @count = (@count + 1)

END

 

All that was really done is a manual 'For' loop using the SQL WHILE loop.  Just set the number in WHILE (@count < 40) to however many times you want the loop to run.

Tags:

sql

Related posts

Comments

3/31/2010 3:21:35 PM

pingback
Pingback from sfan.dyndns.org

MSSQL 迴圈寫法 – 憂藍的風

sfan.dyndns.org

5/6/2010 4:58:10 AM

pingback
Pingback from endyear2012.com

final test pingback from « End Year 2012

endyear2012.com


Comments are closed