How to insert N rows of default values into a table

前端 未结 4 1068
闹比i
闹比i 2021-01-04 19:02

I have a table containing an identity column as well as a column representing the creation date:

CREATE TABLE dbo.OrderStatus
(
    OrderStatusId int IDENTIT         


        
4条回答
  •  攒了一身酷
    2021-01-04 19:25

    The Tally Table method can insert large sets of multiple rows, providing the tally table is big enough. This Tally table will handle up to 1000 entries.

    WITH Tally (n) AS
    (
        -- 1000 rows
        SELECT ROW_NUMBER() OVER (ORDER BY (SELECT NULL))
        FROM (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) a(n)
        CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) b(n)
        CROSS JOIN (VALUES(0),(0),(0),(0),(0),(0),(0),(0),(0),(0)) c(n)
    )
    --SELECT * FROM Tally;
    
    Create Table #temp (id int, d datetime, GUID uniqueidentifier, str1 nvarchar(1), number int)
    
    insert into #temp
    select n, getdate(), newid(), 'a', 101 from tally 
    where N<=100 -- THIS IS WHERE YOU INDICATE HOW MANY ROWS
    
    select * from #temp
    

提交回复
热议问题