T-SQL INSERT INTO Multiple Rows: Step-by-Step Guide
In this article
To insert multiple rows in T-SQL, use a single INSERT INTO ... VALUES statement with comma-separated row constructors, or use INSERT INTO ... SELECT with UNION ALL for larger sets. The single-statement approach is faster and cleaner than running one INSERT per row. For example: INSERT INTO dbo.Products (Name, Price) VALUES ('Widget', 9.99), ('Gadget', 14.50), ('Doohickey', 7.25); inserts three rows in one round trip. This guide covers the syntax, the common pitfalls, and how to verify the results.
What You'll Need
- A SQL Server instance (any recent version) and a client such as SQL Server Management Studio (SSMS), Azure Data Studio, or
sqlcmd. - A database and a target table with the correct columns and data types.
- Permission to insert into the table (typically
INSERTpermission or membership in a role that grants it). - Basic familiarity with T-SQL syntax and the table's schema.
Step-by-Step
- Connect to the right database. In SSMS or Azure Data Studio, open a new query window and select the correct database from the dropdown, or run
USE YourDatabaseName;at the top of the script. This prevents accidentally inserting into a table in the wrong database. - Inspect the target table. Run
EXEC sp_help 'dbo.YourTable';or expand the table in Object Explorer to see column names, data types, nullability, and any defaults. Note which columns areIDENTITY(you cannot insert explicit values withoutSET IDENTITY_INSERT) and which have defaults you can omit. - Write the INSERT with a column list. Always name the columns you are supplying:
INSERT INTO dbo.YourTable (Column1, Column2, Column3). Omitting the column list ties your statement to the table's physical column order, which breaks if the schema changes. - Add the VALUES clause with multiple row constructors. Separate each row with a comma and wrap each row in parentheses:
VALUES ('A', 1, '2024-01-01'), ('B', 2, '2024-01-02'), ('C', 3, '2024-01-03');. The number of values in each row must match the column list exactly. - Match data types and formats. Quote strings and dates with single quotes, use unquoted numbers for numeric columns, and use
NULL(without quotes) for nulls. For dates, use unambiguous formats like'YYYY-MM-DD'or'YYYYMMDD'to avoid locale-dependent parsing. - Run the statement and check the messages tab. SQL Server returns a message like
(3 rows affected). If the count differs from what you expected, stop and investigate before proceeding. - Verify the inserted rows. Query them back with a
SELECTfiltered on a unique value you just inserted, for exampleSELECT * FROM dbo.YourTable WHERE Column1 IN ('A','B','C');. This confirms the data landed as intended. - Wrap larger inserts in a transaction. For scripts that insert many rows or touch multiple tables, use
BEGIN TRANSACTION;...COMMIT TRANSACTION;with error handling (BEGIN TRY ... BEGIN CATCH ... ROLLBACK ... END CATCH) so a failure does not leave partial data behind. - For very large sets, switch to INSERT ... SELECT. Instead of thousands of
VALUESrows, useINSERT INTO dbo.Target (Col1, Col2) SELECT Col1, Col2 FROM dbo.Staging UNION ALL SELECT ...;or insert from a staging table. This is easier to review and often performs better.
Common Mistakes
- Omitting the column list. This silently maps values to columns in table order and breaks when columns are added or reordered.
- Mismatched value counts. Each row constructor must have exactly as many values as there are columns in the list. A missing or extra value causes an error like "Column name or number of supplied values does not match table definition."
- Forgetting parentheses around each row. Writing
VALUES 'A', 1, 'B', 2without per-row parentheses is invalid; each row needs its own set. - Using double quotes for strings. T-SQL uses single quotes for string and date literals. Double quotes are identifiers when
QUOTED_IDENTIFIERis on. - Trying to insert into an IDENTITY column. Explicit values require
SET IDENTITY_INSERT dbo.YourTable ON;(andOFFafterwards), and only one table per session can have it on at a time. - Exceeding the row constructor limit. A single
VALUESclause is limited to 1,000 row constructors. Beyond that, useINSERT ... SELECTor batch the inserts. - Ignoring constraints. Unique keys, foreign keys, check constraints, and NOT NULL columns will reject rows. The whole statement fails if any row violates a constraint (unless you use
IGNORE_DUP_KEYon a unique index, which has its own trade-offs).
Tips
- Format multi-row inserts with one row per line so errors are easy to spot.
- Use a transaction for anything beyond a handful of rows, and test the script on a development copy first.
- Prefer parameterized inserts from application code to avoid SQL injection and to let the plan cache reuse the query plan.
- For bulk loads of thousands of rows, consider
BULK INSERTorbcprather than giantVALUESlists. - Keep a
SELECTverification query next to your insert script so you can confirm results immediately.
Troubleshooting
If the statement fails, read the error message first; it usually names the column or constraint involved.
- "Column name or number of supplied values does not match table definition." Your column list and value count differ. Recount both, row by row.
- "Cannot insert the value NULL into column ..." A NOT NULL column received NULL or was omitted without a default. Supply a value or add a default to the table.
- "Violation of PRIMARY KEY constraint" or "UNIQUE KEY constraint." A duplicate value already exists. Check the table for the conflicting value and adjust or remove the row.
- "The INSERT statement conflicted with the FOREIGN KEY constraint." The referenced parent row does not exist. Insert the parent first or correct the key value.
- "Conversion failed when converting ..." A value does not match the column's data type. Check date formats and numeric strings.
- "Cannot insert explicit value for identity column." Either omit the identity column from the list or wrap the statement with
SET IDENTITY_INSERT ... ON/OFF. - Statement runs but no rows appear. Confirm you are querying the same database and schema, and that you committed the transaction rather than leaving it open.
Frequently Asked Questions
Can I insert multiple rows in one INSERT statement in T-SQL?
Yes. Use a single INSERT INTO ... VALUES with comma-separated row constructors, up to 1,000 rows per statement. For more, use INSERT ... SELECT.
What is the difference between INSERT ... VALUES and INSERT ... SELECT?
VALUES supplies literal rows directly. INSERT ... SELECT pulls rows from another table or query, which is better for large sets, transformations, or data already in the database.
How do I insert multiple rows and get the generated identity values back?
Add an OUTPUT INSERTED.Id clause to the INSERT statement. The inserted identity values are returned as a result set.
Can I insert multiple rows with a single INSERT if one row violates a constraint?
No. By default the entire statement fails and no rows are inserted. Validate data first or insert in smaller batches if partial success is acceptable.
Do I need a transaction for a multi-row insert?
A single INSERT statement is atomic by itself. Use an explicit transaction when you combine multiple statements or want to roll back on a later error.
Why does my multi-row insert run slowly?
Large VALUES lists, triggers, and many indexes add overhead. For bulk data, use BULK INSERT or a staging table with INSERT ... SELECT, and consider disabling nonclustered indexes during the load if appropriate.