Pages

Thursday, November 3, 2011

Running a 'for' loop in ms sql

I just want to insert few rows in to my table at once. It seems complicated for me to do that in a way i want. But i did stumble upon a convenient way for populating lot of new rows using a simple technique that is not using bulk insert. It is actually pretty basic stuff, but useful for my purpose.



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. 

No comments:

Post a Comment