Question

Guys I have a table in database which has around 200+ records. Now for example, if I delete a record which has primary key (identity column) = 25, so my pk column becomes 23,25,26,27...etc

Is there any command that I could use that will update the identity column of whole table? DBCC command is not what I am looking for...neither "set identity_insert YourTable ON" solved my problem.

Do I have to truncate the whole table and re-enter all the data? What if my db had 200k rows ? this solution would be impractical.

P.S. please don't ask why I need to serialize my PK column. Its my client's requirement. I am using SQL Server 2012.

Was it helpful?

Solution

No, Identity column can't be re-serialized. You can just create your own identity column (auto-increment column) using stored procedure (on INSERT). It will work just like Identity column and you will not have to worry about the jumps.

OTHER TIPS

IDENTITY only guarantees that values will be unique, not that they will be serialized (like in your case).

You'll need to copy all data into a temp table and copy it back, there is no way to update IDENTITY column (AFAIK).

If you need to update values you propably should change the design and make it a simple int column and create a primary key on it. It'll solve your future problems.

But you should talk to your client and check what are the priorities and why he needs serialized values if he really needs them. For clients/orders/products and so on and so on it is really a bad practice to 'recycle' the identifiers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top