본문 바로가기
Oracle

[Query] FOR LOOP

by 캡틴노랑이 2016. 1. 8.
반응형

FOR LOOP

FOR x IN (
    WITH KEY_INFO AS (
        SELECT ADD_MONTHS(TO_DATE(P_REQ_DT,'YYYYMMDD'),-1) REQ_DT FROM DUAL
        )
    SELECT CLS_DT
    FROM (
        SELECT TO_CHAR(REQ_DT + LEVEL,'YYYYMMDD') CLS_DT
        FROM   KEY_INFO
        CONNECT BY LEVEL <= 100
        )
    WHERE CLS_DT LIKE SUBSTR(P_REQ_DT,1,6)||'%')
LOOP 
  DELETE FROM TB_CLS WHERE CLS_DT = x.CLS_DT;  
  
  INSERT INTO TB_CLS(CLS_DT, OUT_CST_CD, SBRC_CD, CST_CD, ITEM_CD)
  WITH VI_OUT_CCLS AS(
    SELECT A.REQ_DT
         , A.OUT_CD
         , B.SRC_CD
         , B.CT_CD
         , A.ITM_CD         
    FROM   TB_EQ_DTL   A
         , TB_EQ_MST   B         
    WHERE  A.REQ_NO      = B.REQ_NO
    AND    A.ITEM_CD     = E.ITEM_CD   
    AND    B.SC_CD BETWEEN '111' AND '299'
    AND    A.REQ_DT = x.CLS_DT 
    GROUP  BY A.REQ_DT, A.OUT_CD
  )
  SELECT A.CLS_DT
       , A.OUT_CD
       , A.SBRC_CD
       , A.CST_CD      
       , TO_CHAR(SYSDATE,'YYYYMMDDHH24MISS') WRK_DTM
       , 'Y'                  USE_YN
       , P_USER_ID
       , SYSDATE
  FROM   VI_CLS A
       , VW_DEPT         B
  WHERE  A.SBRC_CD = B.DEPT_CD(+)
  ; 
  COMMIT;
END LOOP;


반응형

댓글