Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting the below error even though i am truncating the table before inserting the data.

Error: Error 8101 An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON

Below is the script i am using.

TRUNCATE TABLE TableName
INSERT INTO TableName SELECT * FROM TableName
Posted

1 solution

Hi,

Please take a look at this:
SQL SERVER – Fix : Error 8101 An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON[^]


Added:
Also, you need to insert your rows using a column list:
SQL
INSERT INTO TableName
(ColumnName(s))
SELECT ColumnName(s)
FROM TableName
 
Share this answer
 
v3
Comments
Yash Thakkar 25-Mar-14 7:52am    
Thanks Andrius but i already have gone thru this link but didn't get the solution which i am looking for. I am truncating the table first then also i am getting this error.
Andrius Leonavicius 25-Mar-14 8:01am    
Truncation deletes all rows and resets the counter for the identity column. But if you want to insert rows containing specific identity values (like your INSERT INTO TableName SELECT * FROM TableName), then you need to use this: SET IDENTITY_INSERT TableName ON. Try this:
TRUNCATE TABLE TableName
SET IDENTITY_INSERT TableName ON
INSERT INTO TableName (Column1, Column2, Column3, ...) SELECT Column1, Column2, Column3, ... FROM TableName
SET IDENTITY_INSERT TableName OFF
Andrius Leonavicius 25-Mar-14 8:08am    
The identity_insert requires specifying all the column names in parenthesis!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900