declare @a int,@b int,@c int,@d int
while 1=1
begin
  select top 1 @a=x.开始编号,@b=x.结束编号,@c=y.开始编号,@d=y.结束编号 
    from 序号表 as x inner join 序号表 as y on y.开始编号=x.结束编号+1
  if @@rowcount=0 break  delete 序号表 where 开始编号=@c and 结束编号=@d
  update 序号表 set 结束编号=@d where 开始编号=@a and 结束编号=@b
end
select * from 序号表
非常希望各位注意:不要受到3GL编程语言的狭隘思维方式的束缚。

解决方案 »

  1.   

    用SQL语言擅长的风格写程序,就像精神正常的人说话一样自然。相比之下,用C++写程序,就像是“祥林嫂”一样徐徐叨叨的,或者就像老夫子一样“迂腐”。
      

  2.   

    假设“开始编号”和“结束编号”没有重复:select * into #ta from 序号表 where 1=2
    while 1=1
    begin
      insert #ta select x.开始编号,y.结束编号
        from 序号表 as x inner join 序号表 as y on y.开始编号=x.结束编号+1
      if not exists(select * from #ta) break  delete 序号表
      insert 序号表 select * from #a
    end
    select * from 序号表如果“开始编号”或者“结束编号”有重复,要首先处理一下,将重复记录只留下一个。