'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
What is the limitation is SQL Server on how many rows can be inserted in a single insert statement ?
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));
Solution 1:[1]
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.
Solution 2:[2]
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
Solution 3:[3]
Although the max is 1000, it's been demonstrated that performance begins to diminish at much smaller numbers. Eugene Philipov wrote a great article exploring this very topic:
To summarize, the author did some very well-designed experimenting and found a sweet spot at around 25. YMMV.
Solution 4:[4]
you can try this
with tempDataTable AS (SELECT *From (VALUES
(18001,79626,'1992-12-11','1993-12-11') -- this is data u want to insert
)x(empNO,sal,frmDate,toDate)) -- tempDataColoumns
INSERT INTO salaries(emp_no,salary,from_date,to_date) SELECT empNO,sal,frmDate,toDate from newData
Remove '--' at the time of query
Solution 5:[5]
There's short workaround to avoid rows limit and still treat it like one statement (all goes in or if there's one error, everything is rolled back)
INSERT INTO tbl_name (a,b)
SELECT 1,2 UNION ALL
SELECT 1,3 UNION ALL
SELECT 1,4 .......
Solution 6:[6]
Dutchman's solution is cool, but it can be simplified. It turns out that you don't need the CTE. I tried it, and it works without the CTE, like this:
INSERT INTO TableName (Column1, Column2) SELECT Id, SomeName
FROM ( VALUES (1, 'A'),(2,'B'),(3,'C')) x (Id, SomeName)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | Dutchman |
Solution 3 | Todd Menier |
Solution 4 | David Buck |
Solution 5 | mr R |
Solution 6 | nospringchicken |