declare @Num int
select @Num=max(num) from tablename
declare @i int
declare @Temp table (Num int)
set @i=1
while @i<=@Num+1 --- 如果全部连续,得到@Num+1
begin
insert @Temp values(@i)
set @i=@i+1
end
select @Num=(
select top 1 num from @Temp a
where not exists (
select * from tablename where num=a.num
)
order by num
)