SQL Server Maximum rows that can be inserted in a single insert statment

前端 未结 5 444
悲哀的现实
悲哀的现实 2020-12-03 21:04

I want to do a batch insert, similar to this question

How to do a batch insert in MySQL

  1. What is the limitation is SQL Server on how many rows can b

5条回答
  •  心在旅途
    2020-12-03 21:21

    The Maximum number of rows you can insert in one statement is 1000 when using INSERT INTO ... VALUES... i.e.

    INSERT INTO TableName( Colum1)
    VALUES (1),
           (2),
           (3),...... upto 1000 rows. 
    

    But if your are using a SELECT statement to insert rows in a table, there is no limit for that, something like...

    INSERT INTO TableName (ColName)
    Select Col FROM AnotherTable
    

    Now coming to your second question. What happens when an error occurs during an insert.

    Well if you are inserting rows using multi-value construct

    INSERT INTO TableName( Colum1)
    VALUES (1),
           (2),
           (3)
    

    In the above scenario if any row insert causes an error the whole statement will be rolled back and none of the rows will be inserted.

    But if you were inserting rows with a separate statement for each row i.e. ...

    INSERT INTO TableName( Colum1) VALUES (1)
    INSERT INTO TableName( Colum1) VALUES (2)
    INSERT INTO TableName( Colum1) VALUES (3)
    

    In the above case each row insert is a separate statement and if any row insert caused an error only that specific insert statement will be rolled back the rest will be successfully inserted.

提交回复
热议问题