SQL Server Tips

How to insert multiple rows in SQL Server

Updated December 3, 20269 min read

Insert many rows in one query. A single tray tips six eggs into an egg carton all at once.

How do you insert multiple rows in SQL Server with one query?

The other day I had a list of rows to add to a table, and I didn't want to write one INSERT per row. Here is how to insert multiple rows in SQL Server in one statement. The best way depends on where the rows are.

How to insert multiple rows in SQL Server: 5 ways

  1. My favorite: multi-row VALUES (simplest): use when I type a short list into a script.
  2. UNION ALL: use when each row needs a calculation.
  3. INSERT ... SELECT: use when the rows are already in a table or query.
  4. VALUES in a derived table: use when I type more than 1,000 rows.
  5. BULK INSERT: use when the rows are in a text file.

Two extras: one transaction for a script that already has one INSERT per row, and OUTPUT to get the new IDs back.

Decision tree for inserting multiple rows in SQL Server: VALUES, derived table, INSERT SELECT or BULK INSERT.
Figure 1: Pick a way to insert many rows, from where the rows are to the T-SQL to use.

Every script uses a small Customers table. Swap in your own names. I made test data so you can try these: jump to it. Run them in order for the same IDs I show. I ran them on SQL 2022.

Multi-row VALUES for a short list

Write the column list once, then one set of parentheses per row.

T-SQL
INSERT INTO dbo.Customers (Name, City)
VALUES (N'Ava',  N'Austin'),
       (N'Ben',  N'Boston'),
       (N'Cara', NULL),
       (N'Dev',  N'Denver');

SELECT CustomerID, Name, City FROM dbo.Customers ORDER BY CustomerID;
CustomerIDNameCity
1AvaAustin
2BenBoston
3CaraNULL
4DevDenver

One statement, four rows. A VALUES list can hold NULL too, as the Cara row shows.

Gotcha: a VALUES list stops at 1,000 rows

One VALUES list holds 1,000 rows at most. 1,000 rows ran. 1,001 rows failed:

Code
Msg 10738, Level 15, State 1
The number of row value expressions in the INSERT statement exceeds the maximum allowed number of 1000 row values.

The failed statement inserted nothing. Microsoft lists this limit on its table value constructor page. The fix is the derived table below.

A 1,001-row VALUES list fails with error 10738, while the same rows in a derived table insert fine.
Figure 2: Error 10738 stops a 1,001 row VALUES list. The same rows in a derived table insert fine.

UNION ALL for calculated rows

Each SELECT makes one row, and UNION ALL stacks them. Good when each row is built from expressions.

T-SQL
INSERT INTO dbo.Customers (Name, City)
SELECT N'Eli', N'Erie'
UNION ALL SELECT N'Fay', N'Fresno'
UNION ALL SELECT N'Gus', NULL;

SELECT CustomerID, Name, City FROM dbo.Customers
WHERE  Name IN (N'Eli', N'Fay', N'Gus') ORDER BY CustomerID;
CustomerIDNameCity
5EliErie
6FayFresno
7GusNULL

Same result as the VALUES list. A long UNION ALL chain is hard to read, so I save it for calculated rows.

INSERT ... SELECT from another table

Copy rows that already exist, like a load from a staging table or an archive job.

T-SQL
INSERT INTO dbo.CustomersArchive (CustomerID, Name, City)
SELECT CustomerID, Name, City
FROM   dbo.Customers
WHERE  Name IN (N'Ava', N'Ben', N'Cara', N'Dev');

SELECT CustomerID, Name, City FROM dbo.CustomersArchive ORDER BY CustomerID;
CustomerIDNameCity
1AvaAustin
2BenBoston
3CaraNULL
4DevDenver

Any SELECT works here: joins, filters, aggregates. Run the SELECT alone first and check the rows.

VALUES in a derived table for 1,000+ rows

Put the VALUES list inside a derived table and select from it. A derived table has no 1,000 row limit. I tested it with 1,500 rows.

T-SQL
INSERT INTO dbo.Big (Name)
SELECT v.Name
FROM (VALUES (N'Row1'), (N'Row2'), (N'Row3') /* ... 1,500 rows ... */) AS v(Name);

SELECT N'dbo.Big' AS table_name, COUNT(*) AS row_count FROM dbo.Big;
table_namerow_count
dbo.Big1500

The list above is shortened. My test list had 1,500 rows, built with code. If your rows are in a file, use the next way.

BULK INSERT from a file

Here is a small file named new_customers.csv:

Code
Name,City
Pam,Provo
Quinn,Quincy
Ray,Reno
T-SQL
BULK INSERT dbo.Imported
FROM 'C:\temp\new_customers.csv'
WITH (FIELDTERMINATOR = ',', ROWTERMINATOR = '0x0a', FIRSTROW = 2);

SELECT Name, City FROM dbo.Imported ORDER BY Name;
NameCity
PamProvo
QuinnQuincy
RayReno

FIRSTROW = 2 skips the header line. The path must work from the machine where SQL Server runs, as the BULK INSERT page says. My test file ends each line with a line feed, so I set ROWTERMINATOR = '0x0a'. Check which line ending your file uses.

One transaction for a script with one INSERT per row

Don't want to rewrite a script with one INSERT per row? Wrap it in a transaction. COMMIT saves the three rows together.

T-SQL
BEGIN TRANSACTION;
INSERT INTO dbo.Log1 (Msg) VALUES (N'one');
INSERT INTO dbo.Log1 (Msg) VALUES (N'two');
INSERT INTO dbo.Log1 (Msg) VALUES (N'three');
COMMIT TRANSACTION;

SELECT N'dbo.Log1' AS table_name, COUNT(*) AS row_count FROM dbo.Log1;
table_namerow_count
dbo.Log13

OUTPUT to get the new IDs

The table has an IDENTITY column and you need the new IDs. SCOPE_IDENTITY() returns only the last one:

T-SQL
INSERT INTO dbo.Customers (Name, City)
VALUES (N'Hal', N'Hull'), (N'Ida', N'Irvine'), (N'Jon', N'Jackson');

SELECT SCOPE_IDENTITY() AS last_identity_only, @@ROWCOUNT AS rows_inserted;
last_identity_onlyrows_inserted
103

OUTPUT returns every new ID in the same statement:

T-SQL
INSERT INTO dbo.Customers (Name, City)
OUTPUT inserted.CustomerID, inserted.Name
VALUES (N'Kim', N'Kent'), (N'Lee', N'Lima'), (N'Max', N'Miami');
CustomerIDName
11Kim
12Lee
13Max

To use the IDs later, send them into a table variable with OUTPUT ... INTO:

T-SQL
DECLARE @new TABLE (CustomerID int, Name nvarchar(50));

INSERT INTO dbo.Customers (Name, City)
OUTPUT inserted.CustomerID, inserted.Name INTO @new
VALUES (N'Ned', N'Nome'), (N'Ola', N'Omaha');

SELECT CustomerID, Name FROM @new ORDER BY CustomerID;
CustomerIDName
14Ned
15Ola

Gotcha: one bad row fails the whole statement

A multi-row INSERT is one statement, so it is all or nothing. Here the second row breaks the NOT NULL rule on Name:

T-SQL
INSERT INTO dbo.Customers (Name, City)
VALUES (N'Good1', N'X'),
       (NULL,     N'Y'),
       (N'Good2', N'Z');
Code
Msg 515, Level 16, State 2
Cannot insert the value NULL into column 'Name', table 'dbo.Customers'; column does not allow nulls. INSERT fails.
The statement has been terminated.

Afterward Customers still held 15 rows, and none was named Good1 or Good2. To load the good rows anyway, filter out the bad ones with a SELECT first (the INSERT ... SELECT way).

Check before you run

  1. Name the columns in the INSERT, so a new column does not break the script.
  2. Run the SELECT part alone first and look at the rows.
  3. Compare "rows affected" with the number you expected.
  4. Past 1,000 typed rows, use a derived table or a file.
  5. Add OUTPUT when you need the new IDs.

Try it on my test data

Run this in a test DB, not in production. City allows NULL. Name does not. Then run the scripts above in order for the same results.

T-SQL
CREATE DATABASE Red9Test_insrows;
GO
USE Red9Test_insrows;
GO

CREATE TABLE dbo.Customers
(
    CustomerID int IDENTITY(1,1) PRIMARY KEY,
    Name       nvarchar(50) NOT NULL,
    City       nvarchar(50) NULL
);

CREATE TABLE dbo.CustomersArchive
(
    CustomerID int NOT NULL PRIMARY KEY,
    Name       nvarchar(50) NOT NULL,
    City       nvarchar(50) NULL
);

CREATE TABLE dbo.Big      (Name nvarchar(50) NOT NULL);
CREATE TABLE dbo.Log1     (Msg  nvarchar(50) NOT NULL);
CREATE TABLE dbo.Imported (Name nvarchar(50) NOT NULL, City nvarchar(50) NULL);

For the BULK INSERT script, save the three-row CSV from above where SQL Server can read it. When you are done, drop the test DB.

Check Red9's SQL Server Services

SQL Server Consulting

Perfect for one-time projects like SQL migrations or upgrades, and short-term fixes such as performance issues or SQL remediation.

Discover More ➜

SQL Server Managed Services

Continuous SQL support, proactive monitoring, and expert DBA help with one predictable monthly fee.

Discover More ➜

Emergency SQL Support

Take the stress out of emergencies with immediate access to a SQL Server Sr. DBA 24x7x365

Discover More ➜

Explore All Services ➜