How can we reset the IDENTITY value?

315    Asked by bhusha_8629 in SQL Server , Asked on Jul 12, 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 IDs go from 68 -> 92 and this crashes my code.

How do I reset the IDENTITY value?


Answered by Ankesh Kumar

You can reset the identity column in SQL server value 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.

  Refer to: SQL SERVER – DELETE, TRUNCATE and RESEED Identity for a detailed example and some good explanation of the difference between Truncate and Delete


Your Answer

Interviews

Parent Categories