Insert multiple rows with explicit columns and batch boundaries
A multirow INSERT lists several parenthesized rows after one VALUES keyword. Use an explicit column list so each value has a stable destination even if the table later gains another column.
This is useful for a small batch of known values. It is not a reason to construct a large SQL string from untrusted input or to assume every database permits an unlimited number of values in one statement.
Start with a complete small fixture
The examples insert three inventory items and then undo the insert. Use a fresh connection without a pending transaction. Each alternative owns only a temporary table, so it does not require an existing application schema.
The expected rows are (10, BOLT, 12), (20, NUT, 8) and (30, SEAL, 5). Every row has three values in the same order as ItemId, Code and Quantity. The final count after rollback is zero.
Insert a batch with SQL Server
CREATE TABLE #InsertDemo (
ItemId int PRIMARY KEY,
Code varchar(20) NOT NULL,
Quantity int NOT NULL CHECK (Quantity >= 0)
);
BEGIN TRY
BEGIN TRANSACTION;
INSERT INTO #InsertDemo (ItemId, Code, Quantity)
VALUES (10, 'BOLT', 12),
(20, 'NUT', 8),
(30, 'SEAL', 5);
SELECT ItemId, Code, Quantity FROM #InsertDemo ORDER BY ItemId;
ROLLBACK TRANSACTION;
END TRY
BEGIN CATCH
IF XACT_STATE() <> 0 ROLLBACK TRANSACTION;
THROW;
END CATCH;
SELECT COUNT(*) AS RowsAfterRollback FROM #InsertDemo;
DROP TABLE #InsertDemo;
SQL Server limits the direct INSERT ... VALUES form to 1,000 rows. The limit belongs to that use of the table value constructor, not to the total size of a table or transaction. A derived table, multiple parameterized batches, table-valued parameters or a bulk-loading API may suit larger inputs. Choose according to the caller and required failure behavior.
Insert a batch with SQLite
Run this in a new in-memory SQLite connection.
CREATE TEMP TABLE InsertDemo (
ItemId INTEGER PRIMARY KEY,
Code TEXT NOT NULL,
Quantity INTEGER NOT NULL CHECK (Quantity >= 0)
);
BEGIN TRANSACTION;
INSERT INTO InsertDemo (ItemId, Code, Quantity)
VALUES (10, 'BOLT', 12),
(20, 'NUT', 8),
(30, 'SEAL', 5);
SELECT ItemId, Code, Quantity FROM InsertDemo ORDER BY ItemId;
ROLLBACK;
SELECT COUNT(*) AS RowsAfterRollback FROM InsertDemo;
DROP TABLE InsertDemo;
SQLite also supports multiple rows after VALUES. Its SQL length and bound-parameter limits are separate constraints, and builds or connections can impose lower limits. Do not copy SQL Server's 1,000-row rule into SQLite. If a statement fails interactively, stop and roll back before continuing the fixture.
Make values and types line up
A missing or extra value is not filled by position guessing. Each row must match the stated column count. Omit a column from the column list when its database default should apply to every inserted row. Explicit NULL is different from asking for a default, and NULL still fails a NOT NULL constraint.
Check type conversion before assembling a heterogeneous batch. SQL Server can choose a common type for a values column and then fail to convert one of its strings. Explicitly typed parameters avoid accidental conversions and keep locale-dependent numeric or date formatting out of the SQL text. String literals use single quotes in these examples.
Choose a failure boundary
One statement reduces round trips, but a series of statements is not automatically one atomic operation. If an application divides a larger input into batches, decide whether one failed batch should undo all batches or only that batch. Use an explicit transaction to implement the chosen boundary and handle rollback in the caller's error path.
Preserve constraint failures unless the operation explicitly defines another policy. Adding an engine-specific ignore or replacement clause can silently change which records exist. For generated identifiers, use the database's supported returned-value mechanism and an explicit mapping, rather than assuming output or identity order matches the input row order.
Source and further reading
This article addresses the problem raised by rits in โInserting multiple rows in a single SQL query?โ on Stack Overflow, licensed under CC BY-SA 4.0 . The question is the topic source. The explanations and examples here are newly authored from the database documentation, rather than reproduced from a community answer.


Author: Herman Schoenfeld