SQL Server Insert Multiple Rows: Guide and Examples
In this article
To insert multiple rows into a SQL Server table in a single operation, use the INSERT INTO statement followed by the VALUES keyword and a comma-separated list of row constructors. For example: INSERT INTO TableName (Col1, Col2) VALUES (Val1, Val2), (Val3, Val4), (Val5, Val6);. This method, known as a multi-row insert or row value constructor, is significantly more efficient than executing individual insert statements for every record because it reduces the number of round-trips between the application and the database server and minimizes transaction log overhead.
What You'll Need
- Access to a SQL Server instance (LocalDB, Express, Standard, or Enterprise).
- A database management tool such as SQL Server Management Studio (SSMS), Azure Data Studio, or a programmatic connection via .NET, Python, or Java.
- A pre-existing table with a defined schema (columns and data types).
- The necessary permissions (INSERT permission) on the target table.
Step-by-Step
- Identify the Target Table and Columns: Before writing the query, confirm the exact name of the table and the specific columns you intend to populate. It is a best practice to explicitly list the columns in your INSERT statement rather than relying on the table's default column order. This prevents the query from breaking if a new column is added to the table later.
- Draft the Basic INSERT INTO Clause: Start your statement with
INSERT INTOfollowed by the table name. Immediately after the table name, open a parenthesis and list the columns you are providing data for, separated by commas. For example:INSERT INTO Employees (FirstName, LastName, Department). - Add the VALUES Keyword: Place the
VALUESkeyword after the column list. This tells SQL Server that you are providing the literal data to be inserted. - Construct the First Row: Open a parenthesis, enter the values for the columns listed in step 2 in the exact same order, and close the parenthesis. Ensure that strings are wrapped in single quotes (e.g., 'John') and numbers are not. Example:
('John', 'Doe', 'Sales'). - Append Additional Rows: To add more rows, place a comma after the first closing parenthesis and repeat the row construction process for every additional record. For example:
('John', 'Doe', 'Sales'), ('Jane', 'Smith', 'Marketing'), ('Robert', 'Brown', 'IT'). - Terminate the Statement: End the entire command with a semicolon. The complete statement should look like this:
INSERT INTO Employees (FirstName, LastName, Department) VALUES ('John', 'Doe', 'Sales'), ('Jane', 'Smith', 'Marketing'), ('Robert', 'Brown', 'IT');. - Execute and Verify: Run the query in your editor. Once the message "(X row(s) affected)" appears, run a
SELECT * FROM TableNamequery to verify that the data was inserted correctly and that no values were shifted into the wrong columns.
Common Mistakes
One of the most frequent errors is a column-value mismatch. This happens when the number of values provided in a row constructor does not match the number of columns specified in the INSERT INTO clause. If you list three columns but only provide two values in one of the rows, SQL Server will throw an error and the entire batch will fail.
Another common mistake is incorrect data type formatting. Forgetting to wrap strings (VARCHAR, NVARCHAR) or dates in single quotes will cause the engine to interpret the value as a column name or a numeric expression, leading to an "Invalid column name" or "Conversion failed" error. Similarly, attempting to insert a string that is longer than the defined length of the column will result in a "String or binary data would be truncated" error.
Users often forget to handle Identity columns. If a table has a column defined as IDENTITY(1,1), SQL Server automatically generates the value. If you try to include the Identity column in your column list and provide a manual value, the statement will fail. You must either omit the Identity column from your list or use SET IDENTITY_INSERT TableName ON before running the query.
Finally, there is the limit on row constructors. SQL Server has a hard limit of 1,000 rows per single INSERT statement using the VALUES constructor. If you attempt to insert 1,001 rows in one go, you will receive an error. For datasets larger than 1,000, you must split the data into multiple batches or use alternative methods like BULK INSERT or the bcp utility.
Tips
To ensure data integrity, wrap your multi-row insert in a Transaction. By using BEGIN TRANSACTION and COMMIT, you ensure that either all rows are inserted or none are. This prevents "partial inserts" where a crash or a constraint violation halfway through the list leaves your database in an inconsistent state. If an error occurs, you can use ROLLBACK to undo the partial changes.
For better performance with very large sets of data, consider using INSERT INTO ... SELECT. If your data already exists in a staging table or a temporary table, you can move it to the final table using INSERT INTO TargetTable (Cols) SELECT Cols FROM SourceTable. This is generally faster than the VALUES constructor for thousands of rows because it operates as a set-based operation.
When dealing with NULL values, be explicit. If a column allows NULLs and you want a specific row to have no value for that column, use the keyword NULL without quotes. Do not use an empty string ('') unless you specifically want a blank string, as these are treated differently in many database configurations.
Always use parameterized queries if you are implementing this in an application (like C# or Python). Hard-coding values into a SQL string makes your application vulnerable to SQL Injection attacks. Use a library that supports batching parameters to maintain the performance of a multi-row insert while keeping the system secure.
Troubleshooting
If your query fails, first check the Error Message provided by SQL Server. If it says "Column name or number of supplied values does not match table definition," count your columns and your values for every single row in the list. A single missing comma or an extra value in the 50th row will crash the entire statement.
If you encounter a Foreign Key Constraint violation, it means you are trying to insert a value into a column that references another table, but that value does not exist in the parent table. Check the parent table to ensure the ID you are inserting actually exists, or insert the parent record first.
If you receive a Primary Key violation (Duplicate Key), it means one of the rows you are inserting contains a value that already exists in a column marked as the Primary Key. You must either remove the duplicate from your list or update the existing record using a MERGE statement instead of an INSERT.
If the query is running extremely slowly, check if the table has a large number of indexes. Every index must be updated for every row inserted. For massive imports, it is sometimes faster to drop the non-clustered indexes, perform the insert, and then rebuild the indexes.
Frequently Asked Questions
Can I insert multiple rows without specifying the column names?
Yes, but it is risky. If you omit the column list, you must provide values for every single column in the table in the exact order they were created. If the table schema changes, your query will break.
What is the maximum number of rows I can insert in one statement?
The limit for the VALUES constructor is 1,000 rows. For more than 1,000 rows, you should use multiple INSERT statements or the BULK INSERT command.
How do I insert multiple rows from another table?
Use the INSERT INTO ... SELECT syntax. This allows you to pull data from one table and push it into another without manually listing every value.
Does inserting multiple rows in one statement affect performance?
Yes, it significantly improves performance by reducing network overhead and reducing the number of times the database has to commit a transaction to the log.
Can I use a variable for the values in a multi-row insert?
Not directly within the VALUES clause. You would need to use dynamic SQL or a Table-Valued Parameter (TVP) to pass a list of rows as a variable into a stored procedure.
What happens if one row in the list fails?
By default, the entire INSERT statement is treated as a single atomic operation. If one row fails (e.g., due to a data type mismatch), none of the rows in that statement will be inserted into the table.