Make SQL Select same row multiple times

后端 未结 11 2141
南方客
南方客 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 17:07

    Repeat rows based on column value of TestTable. First run the Create table and insert statement, then run the following query for the desired result. This may be another solution:

    CREATE TABLE TestTable
    (
     ID INT IDENTITY(1,1),
     Col1 varchar(10),
     Repeats INT
    )
    
    INSERT INTO TESTTABLE
    VALUES ('A',2), ('B',4),('C',1),('D',0)
    
    WITH x AS 
    (
      SELECT TOP (SELECT MAX(Repeats)+1 FROM TestTable) rn = ROW_NUMBER() 
      OVER (ORDER BY [object_id]) 
      FROM sys.all_columns 
      ORDER BY [object_id]
    )
    SELECT * FROM x
    CROSS JOIN TestTable AS d
    WHERE x.rn <= d.Repeats 
    ORDER BY Col1;
    

提交回复
热议问题