开发者

Deleting Identity IDs in SQL

开发者 https://www.devze.com 2023-03-23 04:22 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消