如果楼主同意我的条件,那么用以下语句就可以帮你搞定问题了
//id2是临时创建的自增列
--在查之前创建ID2为自增列(楼主自已写)
select id2 from aa where id2 not in
(
select isnull(b.id2,0) id2 from 
(select * from aa ) a
left join
aa b
on a.id=b.id2
)
--查完后删除临时列ID2(楼主自已写)
很土!!!!!!!!!!
搞定,给分,呵呵

解决方案 »

  1.   

    create view v_number    --创建一个view,里面存放1到9999的数据,如果需要更大请根据自己的情况修改
    as
    select number from 

    select t1.b+t2.b*10+t3.b*100+t4.b*1000 number 
    from 

    select 0 b union all select 1 b union all select 2 b union all 
    select 3 b union all select 4 b union all select 5 b union all 
    select 6 b union all select 7 b union all select 8 b union all select 9 b 
    ) t1, 

    select 0 b union all select 1 b union all select 2 b union all 
    select 3 b union all select 4 b union all select 5 b union all 
    select 6 b union all select 7 b union all select 8 b union all select 9 b 
    ) t2, 

    select 0 b union all select 1 b union all select 2 b union all 
    select 3 b union all select 4 b union all select 5 b union all 
    select 6 b union all select 7 b union all select 8 b union all select 9 b 
    ) t3, 

    select 0 b union all select 1 b union all select 2 b union all 
    select 3 b union all select 4 b union all select 5 b union all 
    select 6 b union all select 7 b union all select 8 b union all select 9 b 
    ) t4 
    ) t5 
    where number<>0
    --用左外连接找到clothid中的cid不在v_number中的数
    select number 
    from v_number left outer join clothid on number = cid
    where cid is null and number <(select max(cid) from clothid)
    order by number
      

  2.   

    回复人: naniel(高手) ( ) 信誉:100  2002-12-19 11:01:00  得分:0 
     Id =8呢﹖建議再換一種思路
     是说我的方法吗,
    我的方法不可能不行的!!!!!!
    不知你试过没有
     
      

  3.   

    先生成临时表,再找:select top (select max(id) from Yourtable) identity(int,1,1) into #temp from 
    (select top 100 from master..sysobjects
    union
    select top 100 from master..sysobjects
    union
    select top 100 from master..sysobjects
    ...
    )
    select id form #temp where id not in (select id from YourTable)
      

  4.   

    To hanxian(寒星(*)终于有了一颗星) 
    你的方法有这样的问题 
    1)首先不能在原表上继续创建标识列 因为每个表只能有一个标识列 必须导入到临时表才行 
    select identity(int,1,1) as newid ,原表中不是id的字段 into #tmp from tb
    2)再按照你的方法 取出的id号只能去一部分 不是完整的 
    比如已经删除了5个数据 只能取出4个id 而最后一个id丢失了
    可以这样看一下
    clothid                     #tmp
    id                           newid  
    1                            1
    2                            2
    4                            3
    6                            4
    7                            5
    10                           6 
    12                           7
    13                           8
    14                           9
    比较结果 id 为11的删除数据 无法找到
      

  5.   

    有一个问题我发现了
    我的前提是你在删数据时,同时把临时列删了,再建回去,
    你所说的问题和我的问题会发生的原因都是因为#TMP最大值小于CLOTHID的其中的一个值,
    为再为它加上一些条件看看
    》1)首先不能在原表上继续创建标识列 因为每个表只能有一个标识列
    为什么不行,它不是主键,如果从可行性方面考虑,是可以的,因为我已试过,
    其它的原因就看楼主最后决定了再看看我的方法。。
    共同研究!!
      

  6.   

    thanks to  铁老大 你的方法可以的
      

  7.   

    declare @a table (a int,b char(10))
    declare @temptable table(a int)
    insert @a values(1,'a')
    insert @a values(5,'b')declare @char  varchar(1000),@i int,@num int
    set @num=(select max(a) from @a)
    set @i=1
    while @i<=@num
    begin
      insert @temptable values (@i)
      set @i=@i+1
    end
    select a from @temptable where a not in (select a from @a)
      

  8.   

    做个删除触发器,把删除的ID放在某个表中。不过这里有个问题,自增ID不连续并不一定就是删除了。插入不成功也会造成不连续。