--建立测试环境
create table temp (id int identity(1,1),A varchar(10) default '')
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
insert temp values(default)
create table #temp (id int)
--删除几条,产生不连续
delete from temp where id=6 or id=8 or id=13
--用游标
declare @p_id int,@e_id int
select @p_id=0,@e_id=0
declare id_cursor CURSOR
for
select id from tempopen id_cursor
fetch next from id_cursor into @p_id
while @@fetch_status=0
begin
fetch next from id_cursor into @e_id
if @e_id-@p_id>1 --判断连续
begin
insert into #temp values(@p_id)
end
set @p_id=@e_id
end
close id_cursor
deallocate id_cursor
--列结果
select id as '不连续开始ID号' from #tempdrop table #temp,temp

解决方案 »

  1.   

    --第一个记录如果算是不连续的记录
    select pro_sec, pro_id, pro_name from product a 
          where not exists(select * from product where pro_sec=a.pro_sec-1)--第一个记录如果算是连续的记录
    select pro_sec, pro_id, pro_name from product a 
          where not exists(select * from product where pro_sec=a.pro_sec-1)
          and a.proc_sec<>(select isnull(min(pro_sec),0) from product)
      

  2.   

    两位回答的太好了,尤其是jyk1970(),简直绝了。对启发我的思维有很大的帮助。谢谢了!1
      

  3.   

    另外我还有点疑或,我做了一个数据库,其中有一个表的字段就类似于我举的例子。我是用DELPHI做的界面,连到SQL SERVER数据库中的。界面上并没有做删除按钮。而且除了我之外,其他人根本不会去直接用数据的,都是通过界面增加和修改数据的。但奇怪的是总是会莫名其妙的出现记录号不 连续的现象,另人费解。
      

  4.   

    <据说>
    有些开发工具的update可以设置为:先delete加再insert的等效操作。
    这种情况下没有使用数据库的update!
      

  5.   

    但是我的情况并不是每次UPDATE都是这样,大多数的时候是正常的。
      

  6.   

    可能有触发器影响,对于自增字段,即使insert语句rollback后,该id号也已经自动增加了。测试:
    if object_id('t1') is not null
        drop table t1
    go
    create table t1(id int identity(1,1),a int)
    go
    create trigger tr_t1 on t1
    for insert
    as
       if exists(select 1 from inserted where a = 10)
       rollbackgoinsert t1 values(1)
    insert t1 values(3)
    go
    insert t1 values(10)    -- 此行会rollback
    go
    insert t1 values(15)
    go
    select * from t1
    /*显示结果为:
    id          a           
    ----------- ----------- 
    1           1
    2           3
    4           15(所影响的行数为 3 行)
    */