Insert multiple rows WITHOUT repeating the “INSERT INTO …” part of the statement?

前端 未结 15 2033
广开言路
广开言路 2020-11-22 09:16

I know I\'ve done this before years ago, but I can\'t remember the syntax, and I can\'t find it anywhere due to pulling up tons of help docs and articles about \"bulk import

15条回答
  •  無奈伤痛
    2020-11-22 09:28

    Using INSERT INTO ... VALUES syntax like in Daniel Vassallo's answer there is one annoying limitation:

    From MSDN

    The maximum number of rows that can be constructed by inserting rows directly in the VALUES list is 1000

    The easiest way to omit this limitation is to use derived table like:

    INSERT INTO dbo.Mytable(ID, Name)
    SELECT ID, Name 
    FROM (
       VALUES (1, 'a'),
              (2, 'b'),
              --...
              -- more than 1000 rows
    )sub (ID, Name);
    

    LiveDemo


    This will work starting from SQL Server 2008+

提交回复
热议问题