sql – 如何在表中找到“漏洞”

我最近继承了一个数据库,其中一个表的主键由编码值组成(Part1 * 1000 Part2).
我将该列标准化,但我无法更改旧值.
所以现在我有
select ID from table order by ID
ID
100001
100002
101001
...

我想找到表中的“洞”(更准确地说,是100000之后的第一个“洞”).
我正在使用以下选择,但是有更好的方法吗?

select /* top 1 */ ID+1 as newID from table
where ID > 100000 and
ID + 1 not in (select ID from table)
order by ID

newID
100003
101029
...

数据库是Microsoft sql Server 2000.我可以使用sql扩展.

解决方法

select ID +1 From Table t1
where not exists (select * from Table t2 where t1.id +1 = t2.id);

不确定这个版本是否会比你原先提到的版本更快.

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...