Smart Systems for Home

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

SQL Server Insert Multiple Rows: Step-by-Step Guide

· 6 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

You can insert multiple rows in SQL Server in a single statement by listing several parenthesized value sets after VALUES, separated by commas. For example: INSERT INTO dbo.Products (Name, Price) VALUES ('Pen', 1.50), ('Notebook', 3.00), ('Eraser', 0.75);. This is the standard, most efficient way to add many rows at once without repeating the INSERT keyword. Before you start, you need a database, a table with the right columns, and permission to insert data (typically INSERT permission on the table).

What You'll Need

  • A SQL Server instance (any recent version supports multi-row VALUES; the syntax has been available since SQL Server 2008).
  • A database and a target table with defined columns and data types.
  • Permission to insert into the table (e.g., INSERT permission, or membership in a role like db_datawriter).
  • A query tool such as SQL Server Management Studio (SSMS), Azure Data Studio, or sqlcmd.

Step-by-Step

  1. Connect to your database. Open SSMS or Azure Data Studio, connect to your SQL Server instance, and select the correct database from the dropdown (or run USE YourDatabaseName;).
  2. Inspect the table structure. Run sp_help 'dbo.YourTable'; or query INFORMATION_SCHEMA.COLUMNS to list column names, data types, nullability, and defaults. This tells you which columns require values and which can be omitted.
  3. Write the INSERT statement. Start with INSERT INTO dbo.YourTable (Column1, Column2, Column3). List the columns explicitly in parentheses. If you omit the column list, you must supply a value for every column in the table in the exact order they appear, which is error-prone.
  4. Add the VALUES clause with multiple rows. After VALUES, write each row as a parenthesized list of values, separated by commas. Example: VALUES ('A', 10, '2024-01-01'), ('B', 20, '2024-01-02'), ('C', 30, '2024-01-03');. Each row must have the same number of values as the column list, and values must match the column data types or be implicitly convertible.
  5. Handle special data types. For strings, use single quotes and double any embedded single quotes (e.g., 'O''Brien'). For dates, use unambiguous formats like 'YYYYMMDD' or 'YYYY-MM-DD'. For NULL, write the keyword NULL without quotes. For uniqueidentifier, use NEWID() or a literal GUID string.
  6. Execute the statement. Press F5 or click Execute. Check the Messages tab for (N rows affected) to confirm how many rows were inserted.
  7. Verify the data. Run a SELECT query filtered to the new rows (e.g., by a unique value or date) to confirm they were inserted correctly.
  8. Wrap in a transaction for safety. If you are inserting many rows or need to roll back on error, use BEGIN TRANSACTION; before the insert and COMMIT; after, with a ROLLBACK; in a TRY...CATCH block if something fails. This prevents partial inserts.

Common Mistakes

  • Mismatched value counts. Each row must have exactly as many values as columns listed. A missing or extra value causes error 109 (“There are more columns in the INSERT statement than values specified”) or 110 (“There are fewer columns in the INSERT statement than values specified”).
  • Forgetting the column list. Without it, you must provide a value for every column, including identity columns (which you cannot insert into unless IDENTITY_INSERT is on). This often leads to errors or unintended data.
  • Data type mismatches. Inserting a string into an integer column or a date in an unrecognized format causes conversion errors. Always match or explicitly convert values.
  • Violating constraints. Duplicate values in a primary key or unique column, NULLs in NOT NULL columns, or foreign key violations will abort the entire statement (unless you use IGNORE_DUP_KEY or similar, which is rarely advisable).
  • Exceeding the row limit. A single INSERT ... VALUES statement can insert up to 1,000 rows. For more, use multiple statements, INSERT ... SELECT, or BULK INSERT.
  • Omitting the schema. Using INSERT INTO YourTable without dbo. can fail if your default schema differs. Always qualify with the schema name.

Tips

  • Use INSERT ... SELECT with a UNION ALL or a derived table when you need to insert more than 1,000 rows or when values come from another query.
  • For very large datasets, consider BULK INSERT or the bcp utility, which are faster than multi-row VALUES.
  • Always wrap multi-row inserts in a transaction if the operation must be atomic; otherwise, a failure partway through leaves earlier rows inserted.
  • Test your statement on a development copy first, especially when inserting into tables with triggers or foreign keys.
  • Use SET NOCOUNT ON; at the start of scripts to suppress “rows affected” messages, which can clutter output.

Troubleshooting

If the insert fails, read the error message carefully. Common errors include:

  • Error 8152: String or binary data would be truncated. A string value is longer than the column's defined length. Shorten the value or alter the column to a larger type.
  • Error 2627: Violation of PRIMARY KEY constraint. You are inserting a duplicate key. Remove the duplicate row or use a different key value.
  • Error 515: Cannot insert the value NULL into column. A NOT NULL column is missing a value. Provide a value or a default.
  • Error 547: INSERT statement conflicted with FOREIGN KEY constraint. A value in a foreign key column does not exist in the referenced table. Insert the parent row first or correct the value.
  • Error 8101: An explicit value for the identity column can only be specified when IDENTITY_INSERT is ON. Either omit the identity column from the insert or enable SET IDENTITY_INSERT dbo.YourTable ON; (and turn it off afterward).

If the statement runs but no rows appear, check that you are querying the correct database and schema, and that you have not rolled back the transaction. Also verify that triggers are not deleting or modifying the rows after insert.

Frequently Asked Questions

Can I insert multiple rows without listing columns?

Yes, but you must supply a value for every column in the table, in the exact order they appear. This is fragile because adding a column later breaks the statement. It is safer to list columns explicitly.

What is the maximum number of rows I can insert with one VALUES clause?

SQL Server allows up to 1,000 rows per INSERT ... VALUES statement. For more, use multiple statements or INSERT ... SELECT.

How do I insert multiple rows and get the generated identity values?

Use the OUTPUT clause: INSERT INTO dbo.YourTable (Name) OUTPUT inserted.Id VALUES ('A'), ('B'); This returns the new identity values.

Can I use a subquery in a multi-row VALUES clause?

No, VALUES only accepts literal values or expressions, not subqueries. Use INSERT ... SELECT instead.

What happens if one row fails?

By default, the entire statement is aborted and no rows are inserted. If you need partial success, insert rows one at a time or use error handling with TRY...CATCH and transactions.

Is multi-row VALUES faster than multiple single-row inserts?

Yes, a single multi-row insert is generally faster because it reduces round trips and logging overhead. However, for very large volumes, BULK INSERT or table-valued parameters may be even faster.