SQL, Auxiliary table of numbers

前端 未结 7 2030
自闭症患者
自闭症患者 2020-11-21 22:24

For certain types of sql queries, an auxiliary table of numbers can be very useful. It may be created as a table with as many rows as you need for a particular task or as a

7条回答
  •  遥遥无期
    2020-11-21 23:06

    Using SQL Server 2016+ to generate numbers table you could use OPENJSON :

    -- range from 0 to @max - 1
    DECLARE @max INT = 40000;
    
    SELECT rn = CAST([key] AS INT) 
    FROM OPENJSON(CONCAT('[1', REPLICATE(CAST(',1' AS VARCHAR(MAX)),@max-1),']'));
    

    LiveDemo


    Idea taken from How can we use OPENJSON to generate series of numbers?

提交回复
热议问题