SQL Select 'n' records without a Table

前端 未结 8 517
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 03:33

Is there a way of selecting a specific number of rows without creating a table. e.g. if i use the following:

SELECT 1, 2, 3, 4, 5, 6, 7, 8, 9, 10


        
相关标签:
8条回答
  • 2020-12-30 04:09
    ;WITH nums AS
        (SELECT 1 AS value
        UNION ALL
        SELECT value + 1 AS value
        FROM nums
        WHERE nums.value <= 99)
    SELECT *
    FROM nums  
    
    0 讨论(0)
  • 2020-12-30 04:11

    The Recursive CTE approach - is realy good.

    Be just aware of performance difference. Let's play with a million of records:

    Recursive CTE approach. Duration = 14 seconds

    declare @start int = 1;
    declare @end int = 999999;
    
    with numbers as 
    (
        select @start as number
        union all
        select number + 1 from numbers where number < @end
    )
    select * from numbers option(maxrecursion 0);
    

    Union All + Cross Join approach. Duration = 6 seconds

    with N(n) as 
    (
        select 1 union all select 1 union all select 1 union all 
        select 1 union all select 1 union all select 1 union all 
        select 1 union all select 1 union all select 1 union all select 1   
    )
    select top 999999
        row_number() over(order by (select 1)) as number 
    from 
        N n1, N n2, N n3, N n4, N n5, N n6;
    

    Table Value Constructor + Cross Join approach. Duration = 6 seconds

    (if SQL Server >= 2008)

    with N as 
    (
        select n from (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) t(n)    
    )   
    select top 999999
        row_number() over(order by (select 1)) as number
    from 
        N n1, N n2, N n3, N n4, N n5, N n6;
    

    Recursive CTE + Cross Join approach. :) Duration = 6 seconds

    with N(n) as 
    (
        select 1 
        union all
        select n + 1 from N where n < 10    
    )   
    select top 999999
        row_number() over(order by (select 1)) as number 
    from 
        N n1, N n2, N n3, N n4, N n5, N n6;
    

    We will get more amazing effect if we try to INSERT result into a table variable:

    INSERT INTO with Recursive CTE approach. Duration = 17 seconds

    declare @R table (Id int primary key clustered);
    
    with numbers as 
    (
        select 1 as number
        union all
        select number + 1 from numbers where number < 999999
    )
    insert into @R 
    select * from numbers option(maxrecursion 0);
    

    INSERT INTO with Cross Join approach. Duration = 1 second

    declare @C table (Id int primary key clustered);
    
    with N as 
    (
        select n from (values (1),(2),(3),(4),(5),(6),(7),(8),(9),(10)) t(n)    
    ) 
    insert into @C 
    select top 999999
        row_number() over(order by (select 1)) as number
    from 
        N n1, N n2, N n3, N n4, N n5, N n6;
    

    Here is an interesting article about Tally Tables

    0 讨论(0)
提交回复
热议问题