The primary key column is often set to auto-increment when constructing a SQL Server database. To achieve this, the IDENTITY property is enabled on the primary key column. The starting value and increment step are specified as parameters for this property. Each time a new record is added and IDENTITY_INSERT is set to OFF, the IDENTITY column value increases by the predefined step, which is usually a number. Additionally, the IDENTITY INSERT property can only be set to ON for one table at a time within a single session.
This article will discuss the error “Cannot insert explicit value for identity column in table
when IDENTITY_INSERT is set to OFF.” This error typically appears when you attempt to manually insert a value into an identity column while IDENTITY_INSERT is OFF.
Attempt to insert data into an identity column with IDENTITY_INSERT set to OFF
To handle the error, learn to set the IDENTITY_INSERT option as shown below. This feature allows you to specify whether an explicit value can be inserted into an identity column:
SET IDENTITY_INSERT <database name>. <schema name>. <table name> { OFF | ON }
The first argument, , is the name of the database containing the table. The second argument, , is the schema to which the table belongs. The third argument,
, specifies the table with the identity column in question.
There are two primary methods to insert data into a table without errors. Both methods serve as solutions to the error described earlier.
If you attempt to insert data with the IDENTITY_INSERT set to OFF , including a value for the identity column in the INSERT statement will trigger the error: “Cannot insert explicit value for identity column in table ‘person’ when IDENTITY_INSERT is set to OFF.”
Execute this code in the query tab:
SET IDENTITY_INSERT person OFF;
INSERT INTO person(ID, first_name, last_name)
VALUES (3, 'Sadia', 'Majeed'),
(4, 'Michel', 'Ronald')
This will result in the following error:
Error caused by inserting data with IDENTITY_INSERT set to OFF
If you omit the ID value while IDENTITY_INSERT is set to ON , you’ll receive the error: “Msg 545, Level 16, State 1, Line 17. An explicit value must be specified for the identity column in table ‘person’ when IDENTITY_INSERT is set to ON.”
Error caused by omitting the primary key ID in the insert statement when IDENTITY_INSERT is ON
When IDENTITY_INSERT is ON , you must explicitly include the primary key ID in the INSERT statement.
Execute this code in the query tab:
SET IDENTITY_INSERT person ON;
INSERT INTO person(ID, first_name, last_name)
VALUES (5, 'Jack', 'Black'),
(6, 'John', 'Wicky')
Inserting data with IDENTITY_INSERT set to ON
This will insert data without error, requiring the user to specify the primary key ID directly. It will not auto-generate as it does when IDENTITY_INSERT is OFF.
When you SET IDENTITY_INSERT ON , it remains active for the entire session, allowing multiple records to be inserted. This setting is session-specific, so opening another query tab requires enabling it again for that window.