Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to reuse the autoincrement value of a primary key in sql server 2012
Posted
Comments
Bernhard Hiller 4-Jun-13 9:07am    
What do you want to do? I fail to understand your question.
Prasad Khandekar 4-Jun-13 9:10am    
Are you talking about missing values?
Sudhakar Shinde 4-Jun-13 9:10am    
reuse or reset? You can not reuse if record with that key is present. If such records are deleted then you need to reset the primary key autoincrement.
kalisiddayya 4-Jun-13 9:20am    
i am talking about the delete record .how to reset the primary key autoincrement?
Maciej Los 4-Jun-13 16:17pm    
What's for?
Please, be more specific and provide more details. Use "Improve question" widget.

When you deleted some old records, their IDs are no longer in use. There is no necessity at all to fill in the gaps left by the deletion, neiter by inserting new records into those places nor by re-adjusting the IDs of other records.
If you want to get the number of existing records in a table, use COUNT:
SQL
SELECT COUNT(ID) AS [Number of records] FROM MYTABLE

OK, There is a trick to add identity values:
SQL
SET IDENTITY_INSERT MYTABLE ON

Thereafter, you can insert your IDs as integer values.
But that ought to be used carefully, under certain circumstances only: e.g. when migrating data from a different database.
 
Share this answer
 
 
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