Skip Navigation Links
Home
Articles
SQL Server 2012
SQL Server 2014
SQL Server 2016
FAQ
Practice Test
Tip of the Day : SQL Server Database Design - Twitter Profile and Followers
Error Messages
Home > SQL Server Error Messages > Msg 8101 - An explicit value for the identity column in table "Table Name" can only be specified when a column list is used and IDENTITY_INSERT is ON.
SQL Server Error Messages - Msg 8101 - An explicit value for the identity column in table "Table Name" can only be specified when a column list is used and IDENTITY_INSERT is ON.

SQL Server Error Messages - Msg 8101

Error Message

Server: Msg 8101, Level 16, State 1, Line 2
An explicit value for the identity column in table 
"Table Name" can only be specified when a column list 
is used and IDENTITY_INSERT is ON.

Causes

This error happens when you are trying to insert a new row into a table that contains an identity column without specifying the columns in the INSERT statement and you are assigning a value to the identity column instead of letting SQL Server assign the value.

To illustrate on how the error can be encountered:

CREATE TABLE [dbo].[Users] (
    [UserID]    INT NOT NULL IDENTITY(1, 1),
    [UserName]  VARCHAR(20) NOT NULL,
    [FirstName] VARCHAR(50) NOT NULL,
    [LastName]  VARCHAR(50) NOT NULL
)
GO

INSERT INTO [dbo].[Users] 
VALUES ( 1, 'superman', 'Clark', 'Kent' ),
       ( 2, 'batman', 'Bruce', 'Wayne' ),
       ( 3, 'ironman', 'Tony', 'Stark' ),
       ( 4, 'captainamerica', 'Steve', 'Rogers'),
       ( 5, 'hulk', 'Bruce', 'Banner'),
       ( 6, 'spiderman', 'Peter', 'Parker' )
GO
Msg 8101, Level 16, State 1, Line 2
An explicit value for the identity column in table 'dbo.Users' can only be specified when a 
column list is used and IDENTITY_INSERT is ON.

Figure 1: Error Message 8101 - Cause of Error Message

If you specified the column names in the INSERT statement, you will get a different error message:

INSERT INTO [dbo].[Users] ( [UserID], [UserName], [FirstName], [LastName] )
VALUES ( 1, 'superman', 'Clark', 'Kent' ),
       ( 2, 'batman', 'Bruce', 'Wayne' ),
       ( 3, 'ironman', 'Tony', 'Stark' ),
       ( 4, 'captainamerica', 'Steve', 'Rogers'),
       ( 5, 'hulk', 'Bruce', 'Banner'),
       ( 6, 'spiderman', 'Peter', 'Parker' )
GO
Msg 544, Level 16, State 1, Line 2
Cannot insert explicit value for identity column in table 'Users' when 
IDENTITY_INSERT is set to OFF.

Figure 2: Error Message 544 - Cannot insert explicit value for identity column in table 'Users' when IDENTITY_INSERT is set to OFF.

Solution / Workaround:

There are two ways of avoiding any of the errors mentioned above.

Solution #1 - Do not include the IDENTITY column

The first option is not to include the identity column in the INSERT statement and let SQL Server assign the next identity value to the row:

INSERT INTO [dbo].[Users] ( [UserName], [FirstName], [LastName] )
VALUES ( 'superman', 'Clark', 'Kent' ),
        ( 'batman', 'Bruce', 'Wayne' ),
        ( 'ironman', 'Tony', 'Stark' ),
        ( 'captainamerica', 'Steve', 'Rogers'),
        ( 'hulk', 'Bruce', 'Banner'),
        ( 'spiderman', 'Peter', 'Parker' )
GO

SELECT * FROM [dbo].[Users]
GO
| UserID | UserName       | FirstName | LastName |
|--------|----------------|-----------|----------|
|      1 | superman       | Clark     | Kent     |
|      2 | batman         | Bruce     | Wayne    |
|      3 | ironman        | Tony      | Stark    |
|      4 | captainamerica | Steve     | Rogers   |
|      5 | hulk           | Bruce     | Banner   |
|      6 | spiderman      | Peter     | Parker   |

Figure 3: Error Message 8101 - Solution 1 - Do not include the IDENTITY column.

Solution #2 - Enable IDENTITY_INSERT

The second option is to enable the IDENTITY_INSERT property for the table.  If you really want to specify the value for the identity column, this option is the one for you.

SET IDENTITY_INSERT [dbo].[Users] ON
GO

INSERT INTO [dbo].[Users] ( [UserID], [UserName], [FirstName], [LastName] )
VALUES ( 1, 'superman', 'Clark', 'Kent' )
GO

SET IDENTITY_INSERT [dbo].[Users] OFF
GO

SELECT * FROM [dbo].[Users]
GO
| UserID | UserName       | FirstName | LastName |
|--------|----------------|-----------|----------|
|      1 | superman       | Clark     | Kent     |
|      2 | batman         | Bruce     | Wayne    |
|      3 | ironman        | Tony      | Stark    |
|      4 | captainamerica | Steve     | Rogers   |
|      5 | hulk           | Bruce     | Banner   |
|      6 | spiderman      | Peter     | Parker   |

Figure 4: Error Message 8101 - Solution 2 - Enable IDENTITY_INSERT

Setting the IDENTITY_INSERT to ON for the table allows explicit values to be inserted into the identity column of a table.  At any given time, only one table in a session can have the IDENTITY_INSERT property set to ON.  If a table already has this property set to ON and a SET IDENTITY_INSERT ON statement is issued on another table, SQL Server will return the following error message:

Msg 8107, Level 16, State 1, Line 2
IDENTITY_INSERT is already ON for table 'dbo.Departments'. 
Cannot perform SET operation for table 'dbo.Users'.

Execute permissions for the SET IDENTITY_INSERT default to the sysadmin fixed server role and the db_owner and db_ddladmin fixed database roles, and the object owner.

Related Articles :