1. 程式人生 > >獲取資料庫中下一個序列號sql

獲取資料庫中下一個序列號sql

  ////獲取資料庫中下一個序列號

通過查出1到當前最大,當前最大+2的結果集合併為一個,在用b is null 找到+1 +2 的兩行,然後用rownum=1 取這兩個中的第一個即+1的 

  select 'E' || trim(to_char(nvl(a, 0), '00000000')) customer_id

  from (select t.a, t1.a b

        from (select rownum a from dual connect by rownum <= (select max(to_number(replace(customer_id, 'E', ''))) + 2 from tb_cus_firm where customer_id like 'E%')) t,

             (select to_number(replace(customer_id, 'E', '')) a from tb_cus_firm where customer_id like 'E%') t1

        where t.a not like '%4%' and t.a = t1.a(+))

 where b is null and rownum = 1