1.建立表 a 保存所有连续的编号
 select a.id
 from    a
 where not exist ( select 1 from b where a.id = b.id )
  and  a.id <= ( select max(id) from b )

解决方案 »

  1.   

    select top 10 id=identity(int,1,1) into # from sysobjects a,sysobjects bdeclare @tb table (id int)
    insert into @tb select 1
    insert into @tb select 3
    insert into @tb select 7
    insert into @tb select 9select a.id from # a left join @tb b 
    on a.id=b.id
    where b.id is null2
    4
    5
    6
    8
    10
      

  2.   

    2、遍历(空ID串变量 += 当前ID如果不在表中)。
      

  3.   

    select top 10 id=identity(int,1,1) into # from sysobjects a,sysobjects bdeclare @tb table (id int)
    insert into @tb select 1
    insert into @tb select 3
    insert into @tb select 7
    insert into @tb select 9select id from #
    where id not in
    (select id from @tb)
      

  4.   

    建一个参造表比较迅捷select top 10 id=identity(int,1,1) into # from sysobjects a,sysobjects bdeclare @tb table (id int)
    insert into @tb select 1
    insert into @tb select 3
    insert into @tb select 7
    insert into @tb select 9select id from #
    where id not in
    (select id from @tb)就这意思
      

  5.   

    老大的:
    if exists
    (
    select * from jobs a
    where job_id>1 and not exists
    (
    select * from jobs
     where job_id=a.job_id-1
    )
    )
    print '存在缺号'
    else
    print '不存在缺号'
      

  6.   


    SELECT TOP 8000 id = identity(int,1,1) INTO # FROM syscolumns a, syscolumns b   select a.id from # a where id (not in select id from tb)
      

  7.   

    我还没有试成功啊
    我的表格是  myuser  主键是 ID 
      

  8.   

    select isnull((select max(id) from myuser where id <a.id),0)+1 as 不存在的起始编号
           ,id-1 as 不存在的结束编号 from myuser a
    where not exists(select 1 from myuser where id = a.id -1)
      

  9.   

    isnull里面是一个查询语句这种形式可以吗?不存在的记录是没有值吧?不是表示值为null吧?我也遇到这种没有值的问题不知道如何处理。