Recently, I tried force a value into the identity column, SQL Server stoped me with the error Cannot insert explicit value for identity column in table when IDENTITY_INSERT is set to OFF.
Cannot insert explicit value for identity column in table
I was executing the following query.
SQL
-- This will throw an error!
INSERT INTO dbo.EmployeesN (EmployeeID, FirstName, LastName)
VALUES (99, 'Michael', 'Johnson');After executing the query above, I got the error shown in the screenshot below.

The Error Message:
Cannot insert explicit value for identity column in table ‘EmployeesN’ when IDENTITY_INSERT is set to OFF.
Solution
The Override: IDENTITY_INSERT
To fix this error, you must toggle a session-level setting called IDENTITY_INSERT.
SQL
First, execute the query below to turn on IDENTITY_INSERT property.
-- 1. Turn on identity insertion
SET IDENTITY_INSERT dbo.EmployeesN ON;After executing the above query, I got the expected output as shown below.

Now, reexecuted the above query and this time the query has been executed successfully as shown below.

Finally, turn it back off immidiately after inserting the record using the following query.
-- 3. Turn it back off immediately
SET IDENTITY_INSERT dbo.EmployeesN OFF;After executing the above query, I got the expected output as shown below.

Pro Tip: Only one table in a session can have IDENTITY_INSERT set to ON at a time. Always turn it off immediately after you are done.
You may also like the following articles:
- Cannot Obtain The Required Interface
- SQL Server Create Table With Identity Column
- SQL Error Code
- Update Stats on Table SQL Server
After working for more than 15 years in the Software field, especially in Microsoft technologies, I have decided to share my expert knowledge of SQL Server. Check out all the SQL Server and related database tutorials I have shared here. Most of the readers are from countries like the United States of America, the United Kingdom, New Zealand, Australia, Canada, etc. I am also a Microsoft MVP. Check out more here.