缺少数字脚本不会工作?

时间:2014-02-20 06:36:37

标签: oracle oracle11g

此脚本将从以下字符串中获取序列,年,月和日... 然后从序列部分找到缺失的数字,如(1111,1112,115,..等) 问题是这个脚本的输出不正确没有DBMS丢失的数字

  

EMP-1111_14_01_01_2141_G1

     

EMP-1112_14_01_01_1612_G1

     

EMP-1115_14_01_01_1109_G1

     

EMP-1116_14_01_01_1315_G1

     

EMP-1118_14_01_01_0910_G2

     

EMP-1121_14_01_01_1105_G1

     

EMP-1111_14_01_01_1120_G2

输出结果应该是这样的

  

缺少编号1113

     

缺少号码1114

     

缺少编号1117

     

缺少编号1118

     

缺少编号1119

     

缺少编号1120

declare 
  v_name          table1.ENAME%TYPE;
  V_seq           NUMBER (4);
  V_Year          number(2);
  V_Month         number (2);
  V_day           number (2);
  max_seq         number(4);
  min_seq         number(4);

  CURSOR List_ENAME_cur IS
  SELECT ENAME from table1
  WHERE status = 2;
begin 

  FOR List_ENAME_rec IN List_ENAME_cur loop
    if REGEXP_LIKE(List_ENAME_cur.ENAME,'emp[-][1-9]{4}[_][1-9]{2}[_][1-9]{2}[_][1-9]{2}[_][0-9]{4}[_][G]["1"]') then 
      V_seq := substr(List_ENAME_cur.ename,5,4);
      V_Year := substr(List_ENAME_cur.ename,10,2);
      V_Month := substr(List_ENAME_cur.ename,13,2);
      V_day := substr(List_ENAME_cur.ename,16,2);


      if min_seq is null or V_seq_FILENAME < min_seq then
        min_seq := V_seq_FILENAME;
        DBMS_OUTPUT.PUT_LINE('Missing number '||min_seq );
      end if;

      if max_seq is null or V_seq_FILENAME > max_seq then
        max_seq := V_seq_FILENAME;
        DBMS_OUTPUT.PUT_LINE('Missing number '||max_seq );
      end if;

    end if;
  end loop;    
  DBMS_OUTPUT.PUT_LINE('max_seq '||max_seq||' min_seq '||min_seq);    
end;

1 个答案:

答案 0 :(得分:0)

参见此示例

WITH ORDH
     AS (SELECT 1111 AS ORDERNO FROM DUAL
         UNION ALL
         SELECT 1112 AS ORDERNO FROM DUAL
         UNION ALL
         SELECT 1115 AS ORDERNO FROM DUAL
         UNION ALL
         SELECT 1116 AS ORDERNO FROM DUAL
         UNION ALL
         SELECT 1118 AS ORDERNO FROM DUAL
         UNION ALL
         SELECT 1121 AS ORDERNO FROM DUAL),
     GOT_NEXT_ORDERNO
     AS (SELECT ORDERNO,
                LEAD ( ORDERNO ) OVER (ORDER BY ORDERNO) AS NEXT_ORDERNO
         FROM   ORDH)
SELECT ORDERNO + 1 AS FROM_NO, NEXT_ORDERNO - 1 AS TO_NO
FROM   GOT_NEXT_ORDERNO
WHERE  ORDERNO + 1 <> NEXT_ORDERNO;

   FROM_NO      TO_NO
---------- ----------
      1113       1114
      1117       1117
      1119       1120

3 rows selected.