I am having a dilemma. I have a table with a column called ID and it is the primary key. It is in automated increments so 1, 2, 3, and so forth and the table is 1-8.
A former programmer deleted row 7 and so now there is a gap between 6 and 8. I need to change the value of 8 to 7.
I have tried several things:
I can not edit ID 8 because it is an identity column
I have tried to query:
set identity_insert mytable ON
then delete the row and re-add and edit the ID as other threads have recommended. However, the automated number kicked in, and now it is row 9 and I still can not edit the ID. I am not sure what I am doing wrong here. Is there a way to do this? Thanks in Advance.
You probably need to ask why it matters that the numbers are sequential with no gaps: SQL's
IDENTITY
type isn't really for this sort of functionality as you're finding out.That said, if you do need to fill the gaps in, you're nearly there -- the other thing you need to do is reseed the identity field with a new starting number. In SQL Server this is achieved with
DBCC CHECKIDENT
.Don't though make this a programmatic thing to fire -- this should be a DBA task only when you need to clean the data up, and it would be fairly rare in practice (in ten years, I've only ever used it once. And that was on a dev database; it's never been used on live environments). If you find you're reseeding regularly then you need to rethink the behaviour of the application.