Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

T-SQL: Insert Multiple Rows into a Table

· 5 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To insert multiple rows into a table in T-SQL, use a single INSERT statement with multiple value tuples: INSERT INTO dbo.Table (col1, col2) VALUES (1, 'a'), (2, 'b'), (3, 'c');. This is the standard, set-based way to add several rows in one round trip. You can also insert multiple rows from another table with INSERT ... SELECT, or from a table-valued constructor. The single-statement approach is faster and cleaner than running separate INSERT statements for each row.

What You'll Need

  • Access to a SQL Server instance (any recent version) and a database where you have INSERT permission on the target table.
  • A client tool such as SQL Server Management Studio (SSMS), Azure Data Studio, or sqlcmd.
  • The target table must already exist, and you need to know its column names and data types.
  • If the table has an identity column, you normally omit that column from the insert list; SQL Server generates the values.

Step-by-Step

  1. Connect and select the database. Open your client, connect to the server, and run USE YourDatabaseName; so the following statements target the right database.
  2. Inspect the table definition. Run sp_help 'dbo.YourTable'; or query INFORMATION_SCHEMA.COLUMNS to confirm column names, data types, nullability, and whether an identity column exists. This prevents type mismatches and missing-column errors.
  3. Write the multi-row INSERT. Use the syntax INSERT INTO dbo.YourTable (Column1, Column2, Column3) VALUES (Value1a, Value2a, Value3a), (Value1b, Value2b, Value3b), (Value1c, Value2c, Value3c);. Each parenthesized tuple is one row; separate tuples with commas. The column list is optional but strongly recommended.
  4. Match data types and order. Values must be supplied in the same order as the column list, and each value must be convertible to the column's type. Quote strings and dates with single quotes (for example '2024-01-15'), and use unambiguous date formats such as YYYYMMDD or ISO 8601.
  5. Handle NULLs and defaults. To insert a NULL, write the keyword NULL (without quotes) for a nullable column. To let SQL Server apply a column default, either omit the column from the column list or use the DEFAULT keyword in the values tuple.
  6. Run the statement and check the result. Execute it. The Messages tab should report the number of rows affected, for example (3 rows affected). Verify with a SELECT filtered to the new rows.
  7. Wrap in a transaction for safety. For production changes, use BEGIN TRANSACTION; before the insert, verify the row count with SELECT @@ROWCOUNT;, then COMMIT; or ROLLBACK;. This lets you undo a mistake before it is permanent.
  8. Insert from another table when needed. If the rows already exist elsewhere, use INSERT INTO dbo.Target (Col1, Col2) SELECT Col1, Col2 FROM dbo.Source WHERE ...;. This is the right tool for copying or transforming many rows at once.

Common Mistakes

  • Forgetting commas between tuples. Each row tuple must be separated by a comma; a missing comma produces a syntax error near the next parenthesis.
  • Mismatched column count. Every values tuple must supply exactly as many values as there are columns in the column list. Mixing counts across tuples is not allowed.
  • Omitting the column list. Relying on table column order is fragile; a schema change can silently shift values into the wrong columns. Always name the columns.
  • Inserting into an identity column. Unless IDENTITY_INSERT is enabled for the table, you cannot supply a value for an identity column. Omit it from the column list.
  • Using double quotes for strings. In T-SQL, single quotes delimit string literals. Double quotes are identifiers when QUOTED_IDENTIFIER is ON, which causes errors or unexpected behavior.
  • Exceeding the row constructor limit. A single VALUES clause is limited to 1,000 tuples. For larger batches, split into multiple statements or use INSERT ... SELECT.
  • Ignoring constraint violations. A single bad row (NULL in a NOT NULL column, duplicate key, foreign key mismatch) causes the entire statement to fail. Check constraints and data first.

Tips

  • Use INSERT ... SELECT with a UNION ALL of literal rows when you need to insert many rows and want to avoid the 1,000-tuple limit, or when values come from a query.
  • For very large loads, consider batching inserts inside a transaction to control log growth, and test on a non-production copy first.
  • Use SET NOCOUNT ON; at the start of scripts to suppress per-statement row-count messages, which speeds up large batches slightly.
  • Always validate with a SELECT after inserting, and use @@ROWCOUNT to confirm the expected number of rows.
  • Keep a scripted, version-controlled insert file for reference data so it can be re-run consistently across environments.

Troubleshooting

If the statement fails, read the error message carefully; it usually names the column or constraint involved. Common fixes:

  • “Column name or number of supplied values does not match table definition” — your column list and values tuple counts differ. Recount both.
  • “Cannot insert explicit value for identity column” — remove the identity column from the column list, or enable SET IDENTITY_INSERT dbo.YourTable ON; temporarily (and turn it off afterward).
  • “String or binary data would be truncated” — a string is longer than the column allows. Shorten the value or widen the column.
  • “Violation of PRIMARY KEY constraint” — a duplicate key exists. Remove the duplicate row or use a different key.
  • “The INSERT statement conflicted with the FOREIGN KEY constraint” — a referenced parent row is missing. Insert the parent first.
  • Conversion errors — check date and numeric formats. Use ISO dates and avoid locale-dependent formats like DD/MM/YYYY.
  • Permission denied — confirm your login has INSERT permission on the table, or ask an administrator.

If a transaction is open and the statement failed, issue ROLLBACK; before retrying to avoid leaving locks in place.

Frequently Asked Questions

Can I insert multiple rows with a single INSERT statement?

Yes. Use one INSERT with multiple VALUES tuples separated by commas. This is the standard T-SQL syntax and is more efficient than separate statements.

Is there a limit to how many rows I can insert at once?

A single VALUES clause supports up to 1,000 tuples. For more rows, use INSERT ... SELECT or split the insert into multiple statements.

How do I insert multiple rows from another table?

Use INSERT INTO Target (Col1, Col2) SELECT Col1, Col2 FROM Source WHERE ...;. The column lists must align in number and compatible types.

Can I insert multiple rows and get the generated identity values back?

Yes. Add an OUTPUT INSERTED.Id clause to the INSERT statement to return the identity values for the inserted rows.

What happens if one row in a multi-row insert violates a constraint?

The entire statement fails and no rows are inserted (unless you handle errors row by row). Validate data or use a transaction to control the outcome.

Do I need to specify column names?

It is optional but strongly recommended. Naming columns makes the statement resilient to schema changes and avoids misaligned values.