select
    min(b.ID) + 1
from
    (select 
         a.ID
     from
         表 a
     where
         not exists(select 1 from 表 where ID = a.ID+1)
     union
     select 
         0
     from
         表
     where
         not exists(select 1 from 表 where ID = 1) b

解决方案 »

  1.   

    select
        min(b.ID) + 1
    from
        (select 
             a.ID
         from
             表 a
         where
             not exists(select 1 from 表 where ID = a.ID+1)
         union
         select 
             0
         from
             表
         where
             not exists(select 1 from 表 where ID = 1) b
      

  2.   

    --如果这个表的数据量不是太大的话可以用以下方法:假设这个表叫做tempgwl,那个字段叫做id
    select id,identity(int,1,1) as ident into #gwl from testgwl  order by id
    select min(ident) from #gwl where id<>ident