Database Error
Cannot insert explicit value for identity column in table 'orders' when IDENTITY_INSERT is set to OFF.
What This Error Means
This error occurs when a developer tries to insert an explicit value into an identity column, which is not allowed when IDENTITY_INSERT is set to OFF.
Why It Happens
This error happens when a developer tries to insert a value into an identity column (a column that automatically generates a unique value) while IDENTITY_INSERT is set to OFF. IDENTITY_INSERT is used to allow or disallow explicit value specification for the identity column of a table.
How to Fix It
- 1To fix this error, you need to either remove the explicit value specification or set IDENTITY_INSERT to ON. Here's an example:
- 2// Before (broken code)
- 3INSERT INTO orders (id, customer_id, order_date) VALUES (1, 123, '2022-01-01');
- 4// After (fixed code)
- 5SET IDENTITY_INSERT orders ON;
- 6INSERT INTO orders (id, customer_id, order_date) VALUES (1, 123, '2022-01-01');
- 7SET IDENTITY_INSERT orders OFF;
Example Code Solution
❌ Before (problematic code)
SQL
INSERT INTO orders (id, customer_id, order_date) VALUES (1, 123, '2022-01-01');✅ After (fixed code)
SQL
SET IDENTITY_INSERT orders ON;
INSERT INTO orders (id, customer_id, order_date) VALUES (1, 123, '2022-01-01');
SET IDENTITY_INSERT orders OFF;Fix for Cannot insert explicit value for identity column in table 'orders' when IDENTITY_INSERT is set to OFF.
Browse Related Clusters
Related SQL Errors
Related SQL Blog Articles
Have a different error? Get an instant explanation.
Explain Another Error