How to fix An explicit value for the identity column error? | Insert statement failed with Error 8101

This Article guide ,

How to fix An explicit value for the identity column error? | or Insert statement failed with Error 8101

What is identity column?

An identity column, also referred to as an auto-increment column or a sequence, is an integral component of a database table that automatically generates a unique numeric value for every new row inserted into the table. The responsibility of generating and managing the value of the identity column typically rests with the database system itself.

The primary purpose of an identity column is to provide a distinctive identifier for each row in a table, obviating the need for users or applications to explicitly specify a value. It is commonly employed as a primary key or a surrogate key in database tables.

Identity columns offer several advantages:

  1. Uniqueness: Each value generated by the identity column is unique within the table, guaranteeing that each row possesses a distinct identifier.
  2. Simplicity: The responsibility of generating identity values is seamlessly managed by the database system, reducing complexity for application developers.
  3. Efficiency: The utilization of identity columns can enhance performance since the database engine can optimize the allocation and retrieval of values.
  4. Data integrity: Identity columns are frequently employed as primary keys, facilitating referential integrity and ensuring dependable relationships between tables.

Why we get this SQL Server Error Messages – Msg 8101 error?

Sometime we have requirement where we want to move data from old table to new table on which identity column enabled. We need to insert all column records with identity values.

When we try to insert records in table having identity column present using below command,

insert into dbo.Comments_Partition
select * from dbo.Comments

then we will get below error,

Msg 8101, Level 16, State 1, Line 1
An explicit value for the identity column in table 'dbo.Comments_Partition' can only be specified when a column list is used and IDENTITY_INSERT is ON.

This error we are getting because , we can not insert records in identity column when identity insert is set to ON.

How to fix An explicit value for the identity column error?

To resolve this error we need to explicitly specify column name in insert query , which will allow to insert identity column values from one table to another.

Error Msg 8101, Level 16, State 1, Line 1
An explicit value for the identity column in table ‘dbo.Comments_Partition’ can only be specified when a column list is used and IDENTITY_INSERT is ON.

This can be resolved by using below query where we will specify column name in insert statement and mark identity insert on.

set identity_insert dbo.Comments_Partition on
--Insert query
insert into dbo.Comments_Partition([Id]
,[CreationDate]
,[PostId]
,[Score]
,[Text]
,[UserId])
select * from dbo.Comments
set identity_insert dbo.Comments_Partition off

Post execution, data inserted successfully.

(3875183 rows affected)

With this you will able to insert records in table having identity column.

You can learn How to write simple SQL queries? 

Leave a Comment