Cannot Insert Explicit Value for Identity Column in Table – Understanding the Error and How to Resolve It
When working with relational databases, especially Microsoft SQL Server, you may encounter the message: “Cannot insert explicit value for identity column in table ‘<table_name>’ when IDENTITY_INSERT is set to OFF.On the flip side, ” This error stops an INSERT statement from completing because the database engine refuses to accept a user‑supplied value for a column that is configured to generate its own values automatically. Below is a full breakdown that explains why the error occurs, shows typical scenarios that trigger it, and provides step‑by‑step solutions you can apply immediately.
Introduction
Identity columns (also known as auto‑number or auto‑increment columns) are a convenient way to guarantee unique primary‑key values without manual intervention. That said, there are legitimate situations—such as data migration, restoring from backup, or fixing gaps—where you need to insert a specific value into that column. The database automatically increments the value each time a new row is added. When you try to do so without first enabling the proper setting, SQL Server raises the “cannot insert explicit value for identity column in table” error.
Understanding the root cause and the correct way to toggle IDENTITY_INSERT will save you time, prevent data integrity issues, and keep your development workflow smooth.
Understanding Identity Columns
An identity column is defined with the IDENTITY(seed, increment) property. For example:
CREATE TABLE Employees (
EmployeeID INT IDENTITY(1,1) PRIMARY KEY,
FirstName NVARCHAR(50),
LastName NVARCHAR(50),
HireDate DATE
);
- Seed – the starting value (here, 1).
- Increment – the amount added for each new row (here, 1).
When IDENTITY_INSERT is OFF (the default), SQL Server:
- Ignores any value you provide for the identity column in an
INSERTstatement. - Generates the next value based on the seed and increment.
- Throws an error if you explicitly list the identity column in the column list and supply a value.
If you need to override this behavior, you must turn IDENTITY_INSERT ON for the target table, insert your explicit value(s), and then turn it back OFF The details matter here. Less friction, more output..
Common Causes of the Error
| Scenario | Why It Triggers the Error |
|---|---|
| Manual INSERT with explicit value | You wrote INSERT INTO Table (ID, …) VALUES (42, …) while IDENTITY_INSERT is OFF. |
| Bulk import (BULK INSERT, BCP, SSIS) | The source file contains a value for the identity column, but the destination table has IDENTITY_INSERT OFF. |
| Data migration scripts | Scripts generated from a source database retain the original identity values; the target table rejects them. |
| Trigger‑based inserts | A trigger fires an INSERT that includes the identity column without enabling IDENTITY_INSERT. Plus, |
| Replication or log shipping | The subscriber/replica tries to apply a transaction that includes an explicit identity value. |
| Using SELECT … INTO | When the destination table is created on the fly, SQL Server treats the source identity as a regular column, but if you later try to insert into an existing identity column, the error appears. |
Not obvious, but once you see it — you'll see it everywhere.
In each case, the database is protecting you from accidentally breaking the identity sequence, which could lead to duplicate keys or gaps that interfere with future auto‑generated values Easy to understand, harder to ignore..
How to Fix the Error – Step‑by‑Step Guide
Below is a practical, repeatable process you can follow whenever you encounter the “cannot insert explicit value for identity column in table” message. The instructions assume you are working with Microsoft SQL Server, but the concepts apply similarly to other RDBMS (MySQL AUTO_INCREMENT, PostgreSQL SERIAL, Oracle SEQUENCE) It's one of those things that adds up..
1. Verify the Table Structure
First, confirm which column is defined as an identity The details matter here..
SELECT COLUMN_NAME,
COLUMNPROPERTY(OBJECT_ID(TABLE_NAME), COLUMN_NAME, 'IsIdentity') AS IsIdentity
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'YourTable';
If IsIdentity returns 1 for a column, that column is the identity Less friction, more output..
2. Turn IDENTITY_INSERT ON
You must have ALTER permission on the table. Run:
SET IDENTITY_INSERT YourTable ON;
Important: Only one table per session can have
IDENTITY_INSERTset to ON at a time. If you need to work with multiple tables, you must switch the setting between them Easy to understand, harder to ignore..
3. Perform the INSERT with Explicit Values
Now you can insert the specific value(s). Include the identity column in the column list and provide the desired value.
INSERT INTO YourTable (IdentityCol, OtherCol1, OtherCol2)
VALUES (42, 'Sample Data', GETDATE());
You can also insert multiple rows:
INSERT INTO YourTable (IdentityCol, OtherCol1, OtherCol2)
VALUES
(42, 'First', GETDATE()),
(43, 'Second', GETDATE()),
(44, 'Third', GETDATE());
4. Turn IDENTITY_INSERT OFF Again
As soon as you finish inserting the explicit values, revert the setting to protect the table:
SET IDENTITY_INSERT YourTable OFF;
Leaving it ON unintentionally can cause future inserts to fail if they try to supply their own identity values, or worse, allow duplicate keys if you’re not careful.
5. Verify the Insert
Run a quick SELECT to confirm the rows appear as expected:
SELECT * FROM YourTable WHERE IdentityCol IN (42, 43, 44);
6. Reseed the Identity (If Needed)
After inserting explicit values, the next auto‑generated value will continue from the last used identity unless you inserted a value higher than the current seed. If you want to avoid gaps, you can reseed:
-- Find the maximum identity value currently in the table
DECLARE @MaxID INT = (SELECT MAX(IdentityCol) FROM YourTable);
-- Reseed the identity to start from the next value
DBCC CHECKIDENT ('YourTable', RESEED, @MaxID);
Note:
DBCC CHECKIDENTrequires appropriate permissions and should be used cautiously in production environments Simple, but easy to overlook..
Preventive Measures
While knowing how to fix the error is essential, adopting habits that reduce the likelihood of encountering it will save you time and frustration.
- Use Column Lists Explicitly
Always specify the column list in yourINSERTstatements. This