试一下吧,未测试select num1,num2 from
(
select lpad(to_number(lag(num2)over(order by num1))+1,8,'0') num1,
lpad(to_number(num1)-1,8,'0') num2
from
  (select num1,num2
  from T
  where num1<=v_end and num2>=v_start
  union all
  select lpad(to_number(v_end)+1,8,'0'),null from dual
  where not exists (select 1 from T where num1<=v_end and num2>=v_end)
)
)where num1<=num2

解决方案 »

  1.   

    lag少写了两个参数,修正下
    select num1,num2 from
    (
    select lpad(to_number(lag(num2,1,v_start)over(order by num1))+1,8,'0') num1,
    lpad(to_number(num1)-1,8,'0') num2
    from
      (select num1,num2
      from T
      where num1<=v_end and num2>=v_start
      union all
      select lpad(to_number(v_end)+1,8,'0'),null from dual
      where not exists (select 1 from T where num1<=v_end and num2>=v_end)
    )
    )where num1<=num2
      

  2.   

    谢谢,本人oracle新手,你这个是不是个存储过程?
      

  3.   

    嗯,谢谢。v_start和v_end是我传入的区间的起始值和结束值是吧
      

  4.   

    又稍稍调整了下,你拿去你的环境执行,有问题的话再说
    语句中的v_start 和v_end 替换为你的起止参数
    select num1,num2 from
    (
    select lpad(to_number(lag(num2,1,to_number(v_start)-1)over(order by num1))+1,8,'0') num1,
    lpad(to_number(num1)-1,8,'0') num2
    from
      (select num1,num2
      from T
      where num1<=v_end and num2>=v_start
      union all
      select lpad(to_number(v_end)+1,8,'0'),null from dual
      )
    )where num1<=num2
    order by num1