Click here to Skip to main content
15,885,954 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to renumber identity column when any row is deleted
Posted

SQL
DBCC CHECKIDENT ("table1", RESEED, 300)



use the above to set the starting index number.
300 will be the starting indexed number when you insert after executing the above query
 
Share this answer
 
v2
AS Griff rigtly pointed out, please try to avoid this scenarion.

I you really need this, please use the ReSeed funtinality in SQL Server

These links will answer your questions

1. Sql-server-reseed-identity-of-table-table-missing-identity-values-gap-in-identity-column]

2. Sql-server-delete-truncate-and-reseed-identity
 
Share this answer
 
Simple rule: Don't.

It will cause more problems that it will solve. The problem is that it is intended that the ID should be unique - so when you delete a row, you would have to renumber all the rows with higher numbers. What if that ID is being used in a different table as a foreign key? All of those must be renumbered as well - including any records which are loaded into the memory of other instances of your application, or any other application using the same database. And what if you get two deletes at the same time from different users? What should the new number sequence be?

If you want sequential numbers, there are other solutions - but do not try to subvert an identity field as it will cause a lot of intermittent problems in production that may not be apparent in development.
 
Share this answer
 
Comments
[no name] 16-Dec-12 0:22am    
I go with Griff
As OrginialGriff don't do this.. Refer MSDN[^]
 
Share this answer
 
EmpID EmpName
1 Arvind
2 Praveen
3 Sunil
4 Naveen

if i delete 2nd record then the preceding record must me renumbered like in below table

EmpID EmpName
1 Arvind
2 Sunil
3 Naveen

plz give your best answer to maintain identity column
 
Share this answer
 

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