如何从一串连续数字中查出丢失了哪个数字?

tongtianming 2004-12-29 02:25:11
有一列是自动编号的,但是其中被人为删掉了几列。我如何知道被删掉的是哪列

select count(*),max(id)
from table1
————————————
820 823

谢谢!
...全文
155 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
tongtianming 2004-12-29
  • 打赏
  • 举报
回复
最后一次up~
谢谢 iswear428和libin_ftsafe(子陌红尘)
tongtianming 2004-12-29
  • 打赏
  • 举报
回复
如果是非常大的数据库,建临时表并作not in 查询效率是不是有些低呢?
tongtianming 2004-12-29
  • 打赏
  • 举报
回复
iswear428的办法到是能解决眼前的问题。不过如果这个表不是820行,而是几百万行呢?

谢谢 iswear428!!

继续往下看有没有更好的办法~
子陌红尘 2004-12-29
  • 打赏
  • 举报
回复
create table #t(id int)

declare @i int
set @i = 1
while (@i < 824)
begin
insert into #t select @i
set @i = @i+1
end

select a.id from #t a where not exists(select * from table1 where id = a.id)

drop table #t
NinGoo 2004-12-29
  • 打赏
  • 举报
回复
好问题,搬凳子坐下,看下面的
iswear428 2004-12-29
  • 打赏
  • 举报
回复
--构建一个临时表,存储自动编号
declare @max int
declare @min int

select max(id) into @max,min(id) into @min
from table1

declare @temp table(id )
while @max>=@min
begin
insert into @temp values(@max)
set @max =@max-1
end

--获得被删除的ID
select id
from @temp
where id not in(select id from table1)



anruky 2004-12-29
  • 打赏
  • 举报
回复
写个存储进程就可以做到。用游标
子陌红尘 2004-12-29
  • 打赏
  • 举报
回复
如果编号ID > 823的记录被删除了,是无法找出来的,:P

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧