Make SQL Select same row multiple times

后端 未结 11 2171
南方客
南方客 2020-12-11 16:02

I need to test my mail server. How can I make a Select statement that selects say ID=5469 a thousand times.

11条回答
  •  再見小時候
    2020-12-11 16:56

    Here's a way of using a recursive common table expression to generate some empty rows, then to cross join them back onto your desired row:

    declare @myData table (val int) ;
    insert @myData values (666),(888),(777) --some dummy data
    
    ;with cte as
    (
        select 100 as a
        union all
        select a-1 from cte where a>0 
            --generate 100 rows, the max recursion depth
    
    )
    ,someRows as
    (
    select top 1000 0 a from cte,cte x1,cte x2 
           --xjoin the hundred rows a few times
           --to generate 1030301 rows, then select top n rows
    )
    select m.* from @myData m,someRows where m.val=666
    

    substitute @myData for your real table, and alter the final predicate to suit.

提交回复
热议问题