当前位置: 代码迷 >> Sql Server >> 搜索列里面的从1到100000中没有出现的数字!该怎么解决
  详细解决方案

搜索列里面的从1到100000中没有出现的数字!该怎么解决

热度:58   发布时间:2016-04-27 15:40:05.0
搜索列里面的从1到100000中没有出现的数字!
t1

id
1
2
3
5
...

100000


我想搜索这列里面的从1到100000中没有出现的数字,比如上面的缺少4,则查询结果中应该包含4

------解决方案--------------------
declare @i int
set @i = 1

while @i <= 10000 begin
if not exists(select 1 from t1 where id = @i)
print rtrim(@i)
select @i = @i + 1
end;
------解决方案--------------------
select top 100000 identity(int,1,1) as ID into #tp
from syscolumns a ,syscolumns b


select *
from t1
where id not in (select id from #tp)

drop table #tp

------解决方案--------------------
select *
from t1
where id not in (select id from t2)

------解决方案--------------------
我猜想楼主你的意图,可能是想在ID列查找未用的最小数值,然后把它赋给新记录作为唯一性键值吧?
  相关解决方案