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
Nested SQL While Loop SyntaxStep 1: First, it checks for the condition inside the first While loop. Step 2: It will verify the condition in the Nested SQL While Loop (second While loop). If the result is True, the code inside the second While loop begin… end will execute.
INSERT-SELECT-UNION query to insert multiple records Thus, we can use INSERT-SELECT-UNION query to insert data into multiple rows of the table. The SQL UNION query helps to select all the data that has been enclosed by the SELECT query through the INSERT statement.
First, create two table variables - and store in them 1-5, and 1-53 (for days and weeks). Then cartesian join the two and you're done! Change the last SELECT statement into an insert into your destination table and you're done.
You may try it like this:
DECLARE @i int = 0 WHILE @i < 300 BEGIN SET @i = @i + 1 /* your code*/ END
DECLARE @first AS INT = 1 DECLARE @last AS INT = 300 WHILE(@first <= @last) BEGIN INSERT INTO tblFoo VALUES(@first) SET @first += 1 END
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With