I have inserted records into a SQL Server database table. The table had a primary key defined and the auto increment identity seed is set to “Yes”. This is done primarily because in SQL Azure, each table has to have a primary key and identity defined.
But since I have to delete some records from the table, the identity seed for those tables will be disturbed and the index column (which is auto-generated with an increment of 1) will get disturbed.
How can I reset the identity column after I deleted the records so that the column has sequence in ascending numerical order?
The identity column is not used as a foreign key anywhere in database.
issuing 2 command can do the trick
the first reset the identity to zero , and the next will set it to the next available value -- jacob
First : Identity Specification Just : "No" >> Save Database Execute Project
After then : Identity Specification Just : "YES" >> Save Database Execute Project
Your Database ID, PK Start from 1 >>
@jacob
Worked for me, I just had to clear all entries first from the table, then added the above in a trigger point after delete. Now whenever i delete an entry is taken from there.
Run this script to reset the identity column. You will need to make two changes. Replace tableXYZ with whatever table you need to update. Also, the name of the identity column needs dropped from the temp table. This was instantaneous on a table with 35,000 rows & 3 columns. Obviously, backup the table and first try this in a test environment.
This will set the current identity value to 0.
On inserting the next value, the identity value get incremented to 1.