hi
i have a table named users which have a ID column as primary key(is identity=true).
when i add some data to table everything is ok but when delete some of them,
the deleted IDs cause Irregularity in ID column.i want to reset the ID column to begin from 1
and increment 1 per each data without remove existing data.
for example:
before delete : ID 1 2 3 4 5 6 7 8 9
after delete : ID 1 2 4 5 8 9
what should i do???
thanks


Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 17, 2015, 7:36 PM EDT
does not delete any record from the table. it just rest running identity value.
one more point you cannot update column value which define as Identity.
hope this will help you.
Rahul BansalRank #143 · 15.9K reputationPosted Mar 16, 2015, 7:30 PM EDT
create table table1
(
ID int PRIMARY key IDENTITY(1,1),
Name varchar(10)
)
In (1,1) , first 1 is starting value and second 1 means the incremental value.
To reset the identity column use
truncate table table1
which will delete all data of your table.
copy the data of the table into another table before truncate the table and then move the data.
Rizwan AliRank #451 · 5.6K reputationPosted Mar 16, 2015, 7:34 PM EDT
Vikram AgrawalRank #4,713 · 329 reputationPosted Mar 16, 2015, 7:51 PM EDT
as per your requirement you need to update ID column after each deletion.
You need to write little logic for this in the Delte SP.
Thanks
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 16, 2015, 8:00 PM EDT
Hi,
Update column value in identity column is not possible
Agree with Rahul solution, this is one way...
If Id column is foreign key for other table, truncate table is not work... in this case you can use DBCC CHECKIDENT command
check following link
https://msdn.microsoft.com/en-us/library/ms176057.aspx
hope this will help you.
Abolfazl155 reputationPosted Mar 17, 2015, 6:38 AM EDT
i used DBCC CHECKIDENT but it delete all data in table, i don't want it.
i'm looking for a way that reset ID column without delete my data, maybe i should use a temp table and store my data before use DBCC CHECKIDENT as Rahul said.
Vikram Agrawal can you tell me how to update id column?
thanks