Deleting Identity IDs in SQL
If I have ID column, which is a primary key and Identity is set to TRUE, inserting each row should increment my ID. My question is, if I have 100 rows and a 开发者_如何学编程function (somewhere in the code) to select random row (rand(1,101)), how to deal with a situation, when I delete a row and random function fails if the one was selected. I could manage this exception in the code, but wonder if there is a way in SQL to fill these deleted row gaps with continues IDs.
Guessing SQL Server because of "Identity"
Use TOP 100.. ROW_NUMBER()
instead to generate 100 contiguous values and filter on that value.
Or use TOP 1..ORDER BY NEWID()
to choose a random row.
Internal IDENTITY values have no meaning except as a surrogate key. Note an INSERT fail also increments the IDENTITY value, not just deletes.
精彩评论