How to Reset IDENTITY value?

624    Asked by anurhea in SQL Server , Asked on Jul 19, 2021

 I have a table with an IDENTITY column. While developing I delete the rows from time to time and add them again. But the IDENTITY values always kept increasing and didn't start from 1 when I added them again. Now my id's go from 68 -> 92 and this crashes my code.How do I reset the IDENTITY value?

Answered by ankur Dwivedi

You can reset the identity column in SQL server by

  DBCC CHECKIDENT('tableName', RESEED, 0)

So next time you insert into TableName, the identity value inserted will be 1. When you delete rows from the table, it will not reset the Identity value, but it will keep increasing it. Just like what happened in your case. Now when you truncate the table, it will reset the Identity value to its original Seed value of the table. Hope this helps you solve the reset identity column in sql server!



Your Answer

Interviews

Parent Categories