不想用递增,怕有一天记录过多超出范围会出错你用bigint做增量列的类型从 -2^63 (-9223372036854775808) 到 2^63-1 (9223372036854775807) 的整型数据(所有数字)。存储大小为 8 个字节。

解决方案 »

  1.   

    那么想取得最小的空值,sql查询怎么写呢?
      

  2.   

    declare @t  table(id int)
    insert into @t values (1)
    insert into @t values (2)
    insert into @t values(3)
    insert into @t values(4)
    insert into @t values(6)
    insert into @t values(7)
    insert into @t values(8)
    insert into @t values(10)
    declare @n int,
                 @no int,
                   @no1 int
    select @no=count(*) from @t
    set @n=1
    while @n<=@no
    begin
     select @no1= count(*)  from @t where id=@n
    if @no1=0
    begin
    select @n as 最小值
    set @n=@no+1
    end
    else
    begin
    set @n=@n+1
    end
    end
      

  3.   

    select min(id) from (
    select (select count(id)+1 from tablename where id<a.id)as ids,*
    from tablename as a)as u
    where ids<>id
      

  4.   

    select min(id) + 1 from table a 
    where not exists(select 1 from table where id = a.id + 1)
      

  5.   

    谢谢各位。
    aierong(皑婀瑢-数据库XML.NET联盟会局长)   结果不太对啊
      

  6.   

    to zarge
    你的做法基本上满足要求,但如果是删除了第一条记录,则没有机会使用这个记录的编号!
      

  7.   

    感谢 flyhorse1980(飞马) 
    提点,现在作以下修正:
    select (case when isnull((select min(id) as id from 物品 where 编号=1),0)=0 then 1 else isnull(min(编号),0)+1 end) as ID from 物品 a where not exists(select 1 from 物品 where 编号=a.编号+1)