Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 be inserted in a single insert statement ?

  2. What happens when for example the first value is inserted but the second one causes a primary key violation. Are the all INSERT statements rolled back?

INSERT INTO tbl_name (a,b)  VALUES (1, 2), (1, 3)); 
like image 488
Shachaf.Gortler Avatar asked May 26 '16 22:05

Shachaf.Gortler


People also ask

How many rows can you insert in SQL?

The number of rows that you can insert at a time is 1,000 rows using this form of the INSERT statement. If you want to insert more rows than that, you should consider using multiple INSERT statements, BULK INSERT or a derived table.

How can I insert more than 1000 rows in SQL Server?

A table can store upto 1000 rows in one insert statement. If a user want to insert multiple rows at a time, the following syntax has to written. If a user wants to insert more than 1000 rows, multiple insert statements, bulk insert or derived table must be used.

Can we insert several rows into a table with a single insert statement?

The INSERT statement also allows you to insert multiple rows into a table using a single statement as the following: INSERT INTO table_name(column1,column2…) VALUES (value1,value2,…), (value1,value2,…), … In this form, you need to provide multiple lists of values, each list is separated by a comma.


2 Answers

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.

like image 194
M.Ali Avatar answered Sep 22 '22 03:09

M.Ali


You can actually pass in an unlimited number of records using a subquery.

;WITH NewData AS (SELECT * FROM ( VALUES  (1, 'A'),(2,'B'),(3,'C')) x (Id, SomeName)) INSERT INTO TableName (Column1, Column2) SELECT Id, SomeName FROM NewData 
like image 40
Dutchman Avatar answered Sep 20 '22 03:09

Dutchman