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

后端 未结 5 585
半阙折子戏
半阙折子戏 2021-02-02 06:35

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 @Co         


        
5条回答
  •  深忆病人
    2021-02-02 06:57

    Found some different answers that I combined to solve simulair problem:

    CREATE TABLE nummer (ID INTEGER PRIMARY KEY, num, text, text2);
    WITH RECURSIVE
      for(i) AS (VALUES(1) UNION ALL SELECT i+1 FROM for WHERE i < 1000000)
    INSERT INTO nummer SELECT i, i+1, "text" || i, "otherText" || i FROM for;
    

    Adds 1 miljon rows with

    • id increased by one every itteration
    • num one greater then id
    • text concatenated with id-number like: text1, text2 ... text1000000
    • text2 concatenated with id-number like: otherText1, otherText2 ... otherText1000000

提交回复
热议问题