Oracle使用存储过程实现日期分段(按日分段,按月分段)

CREATE OR REPLACE PROCEDURE DIVIDE_BY_MON_DAY(START_DATE IN DATE,
                                              END_DATE   IN DATE) IS
BEGIN

  FOR D IN (SELECT START_DATE + (LEVEL - 1) CUR_DATE
              FROM DUAL
            CONNECT BY LEVEL <= END_DATE - START_DATE) LOOP
  
    DBMS_OUTPUT.PUT_LINE(D.CUR_DATE);
    DBMS_OUTPUT.PUT_LINE(D.CUR_DATE + 1);
    DBMS_OUTPUT.PUT_LINE('------------------------------');
  END LOOP;

  FOR M IN (SELECT ADD_MONTHS(TRUNC(START_DATE, 'mm'), (LEVEL - 1)) CUR_MON
              FROM DUAL
            CONNECT BY LEVEL <= MONTHS_BETWEEN(END_DATE, START_DATE)) LOOP
  
    DBMS_OUTPUT.PUT_LINE(M.CUR_MON);
    DBMS_OUTPUT.PUT_LINE(ADD_MONTHS(M.CUR_MON, 1));
    DBMS_OUTPUT.PUT_LINE('------------------------------');
  END LOOP;

END DIVIDE_BY_MON_DAY;

 

posted @ 2019-08-16 09:15  e3tB8Wz7  阅读(1124)  评论(0编辑  收藏  举报