declare @i int
select @i=max(id) from yourtable
exec('select top '+@i+' id=identity(int,1,1) into tmp from syscolumns a,syscolumns b')select t.id
from tmp t
where not exists(select 1 from yourtable where id=t.id)

解决方案 »

  1.   

    要找出所有漏掉的可以,还是得用循环,如果只是查出是否有漏的就好做了。select max(id)-min(id)+1 from TableName
    select count(id) from TableName看这两个值是否相等
      

  2.   

    mislrb(上班看看早报,上上CSDN,下班看看电影):问题是流水号字段未启用identity标识,而且都是varchar类型的,都是8位数字,不补的前面补0,都是00000001,00000002...,老兄的方法能解决这种情况吗?
      

  3.   

    流水号没有规则,就是递增的号码,比如1,2,3,4,6,7,10,20则要取出5,8,9,11,12,13,14,15,16,17,18,19--------------------------------------
    你自己说这样的啊,如果是'0000000N'这样当然也能解决做些变化:declare @i int
    select @i=max(id) from yourtable
    exec('select top '+@i+' id=identity(int,1,1),nid='00000000' into tmp from syscolumns a,syscolumns b')update tmp set nid=right(nid+cast(id as varchar(10)),8)select t.nid
    from tmp t
    where not exists(select 1 from yourtable where id=t.nid)
      

  4.   

    --如果tmp的数据量达不到@i的值,哪也只能用循环了declare @i int
    declare @tmp table(nid varchar(8) primary key)select @i=max(id) from yourtable
    while @i>0
    begin
      insert @tmp select right('00000000'+cast(@i as varchar(10)),8)
      set @i=@i-1
    endselect t.id
    from @tmp t
    where not exists(select 1 from yourtable where id=t.id)
      

  5.   

    mislrb(上班看看早报,上上CSDN,下班看看电影):exec('select top '+@i+' id=identity(int,1,1),nid='00000000' into tmp from syscolumns a,syscolumns b')我想问一下以上SQL中 from syscolumns a,syscolumns b,是代表什么意思,为什么取两次?