--插入时
declare @id int
select @id=isnull((select max(ID) from 表),1)--删除时重新调整顺序
update t
set ID=(select count(1) from 表 where ID<=t.ID)
from 表 t

解决方案 »

  1.   

    --生成测试用表
    select top 10 identity(int,1,1) id into #1 from sysobjects --那只能一个个检查
    Declare @Next  int
    select  @Next=max(id)+1 
    from #1 a
    where id=(select count(1) from #1 b where a.id>=b.id)select @Next
    drop table #1
      

  2.   

    自定义一个函数
    create function getno()returns int
    as
    begin
    declare @f1 int,@f2 int
    set @f2=1
    Declare Cur_t  CURSOR FOR
    SELECT 你的编号 FROM 你的表 order by 你的编号
    OPEN Cur_t 
    FETCH NEXT FROM  Cur_t INTO @f1
    WHILE @@FETCH_STATUS=0
    begin
    if @f1<>@f2
    BREAK
    set @f2=@f2+1
    FETCH NEXT FROM  Cur_t INTO @f1
    end
    --释放游标
    CLOSE Cur_t
    --删除游标
    DEALLOCATE Cur_t 
    return( @f2)
    end
    调用
    select dbo.getno()