How can INSERT INTO a table 300 times within a loop in SQL?

pencilCake picture pencilCake · Feb 21, 2014 · Viewed 112.3k times · Source

I would like to insert a value retrieved from a counter in SQL and repeat it 300 times.

Something like:

DECLARE @Counter = 0;

-- BEGIN Loop 
    SET @Counter = @Counter + 1 
    INSERT INTO tblFoo VALUES(@Counter)
-- REPEAT 300 times

How can I achieve this? Thanks

Answer

Rahul Tripathi picture Rahul Tripathi · Feb 21, 2014

You may try it like this:

DECLARE @i int = 0
WHILE @i < 300 
BEGIN
    SET @i = @i + 1
    /* your code*/
END