What is the best way to create and populate a numbers table?

后端 未结 11 2680
悲&欢浪女
悲&欢浪女 2020-11-21 13:28

I\'ve seen many different ways to create and populate a numbers table. However, what is the best way to create and populate one? With \"best\" being defined from most to l

11条回答
  •  耶瑟儿~
    2020-11-21 13:55

    For anyone looking for an Azure solution

    SET NOCOUNT ON    
    CREATE TABLE Numbers (n bigint PRIMARY KEY)    
    GO    
    DECLARE @numbers table(number int);  
    WITH numbers(number) as  (   
    SELECT 1 AS number   
    UNION all   
    SELECT number+1 FROM numbers WHERE number<10000  
    )  
    INSERT INTO @numbers(number)  
    SELECT number FROM numbers OPTION(maxrecursion 10000)
    INSERT INTO Numbers(n)  SELECT number FROM @numbers
    

    Sourced from the sql azure team blog http://azure.microsoft.com/blog/2010/09/16/create-a-numbers-table-in-sql-azure/

提交回复
热议问题