--Author: Venkata Potluri
--Date: April 04, 2015

DECLARE
   TYPE T_MEM_ID IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.MEMBER_ID%TYPE;

   TYPE T_LTPD
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_LAST_TXN_PROCED_DT%TYPE;

   TYPE T_LATD
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_LAST_ACCR_TXN_DT%TYPE;

   TYPE T_LRTD
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_LAST_RDM_TXN_DT%TYPE;

   TYPE T_PTAV
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_POINT_TYPE_A_VAL%TYPE;

   TYPE T_PTBV
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_POINT_TYPE_B_VAL%TYPE;

   TYPE T_PTCV
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_POINT_TYPE_C_VAL%TYPE;

   TYPE T_PTDV
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_POINT_TYPE_D_VAL%TYPE;

   TYPE T_PTEV
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_POINT_TYPE_E_VAL%TYPE;

   TYPE T_PTFV
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_POINT_TYPE_F_VAL%TYPE;

   TYPE T_PTGV
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_POINT_TYPE_G_VAL%TYPE;

   TYPE T_PTHV
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_POINT_TYPE_H_VAL%TYPE;

   TYPE T_PTIV
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_POINT_TYPE_I_VAL%TYPE;

   TYPE T_PTJV
      IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.POST_POINT_TYPE_J_VAL%TYPE;

   ARR_MEM_ID   T_MEM_ID;
   ARR_LTPD     T_LTPD;
   ARR_LATD     T_LATD;
   ARR_LRTD     T_LRTD;
   ARR_PTAV     T_PTAV;
   ARR_PTBV     T_PTBV;
   ARR_PTCV     T_PTCV;
   ARR_PTDV     T_PTDV;
   ARR_PTEV     T_PTEV;
   ARR_PTFV     T_PTFV;
   ARR_PTGV     T_PTGV;
   ARR_PTHV     T_PTHV;
   ARR_PTIV     T_PTIV;
   ARR_PTJV     T_PTJV;

   LOG_ROW_ID   NUMBER := 0;
   ROW_COUNT    NUMBER := 0;

   CURSOR C1
   IS
      SELECT /*+ parallel(M,12) full(M) */ M.ROW_ID,
             M.LAST_TXN_PROCED_DT,
             M.LAST_ACCR_TXN_DT,
             M.LAST_RDM_TXN_DT,
             M.POINT_TYPE_A_VAL,
             M.POINT_TYPE_B_VAL,
             M.POINT_TYPE_C_VAL,
             M.POINT_TYPE_D_VAL,
             M.POINT_TYPE_E_VAL,
             M.POINT_TYPE_F_VAL,
             M.POINT_TYPE_G_VAL,
             M.POINT_TYPE_H_VAL,
             M.POINT_TYPE_I_VAL,
             M.POINT_TYPE_J_VAL
        FROM SIEBEL_PURGE.PURGE_MEM_INFO P
             INNER JOIN SIEBEL.S_LOY_MEMBER M ON P.MEMBER_ID = M.ROW_ID;
BEGIN
   SELECT CASE WHEN MAX (ROW_ID) IS NOT NULL THEN MAX (ROW_ID) + 1 ELSE 1 END
     INTO LOG_ROW_ID
     FROM SIEBEL_PURGE.PURGE_LOG;

   INSERT INTO SIEBEL_PURGE.PURGE_LOG (ROW_ID, PROCESS_NAME, START_TIME)
      SELECT LOG_ROW_ID, 'Populate PURGE_MEM_INFO After Purge', SYSDATE
        FROM DUAL;

   COMMIT;

   OPEN C1;

   LOOP
      FETCH C1
         BULK COLLECT INTO ARR_MEM_ID,
              ARR_LTPD,
              ARR_LATD,
              ARR_LRTD,
              ARR_PTAV,
              ARR_PTBV,
              ARR_PTCV,
              ARR_PTDV,
              ARR_PTEV,
              ARR_PTFV,
              ARR_PTGV,
              ARR_PTHV,
              ARR_PTIV,
              ARR_PTJV
         LIMIT 10000;
		 --LIMIT 50;

      FORALL ROW IN ARR_MEM_ID.FIRST .. ARR_MEM_ID.LAST
         UPDATE SIEBEL_PURGE.PURGE_MEM_INFO
            SET POST_LAST_TXN_PROCED_DT = ARR_LTPD (ROW),
                POST_LAST_ACCR_TXN_DT = ARR_LATD (ROW),
                POST_LAST_RDM_TXN_DT = ARR_LRTD (ROW),
                POST_POINT_TYPE_A_VAL = ARR_PTAV (ROW),
                POST_POINT_TYPE_B_VAL = ARR_PTBV (ROW),
                POST_POINT_TYPE_C_VAL = ARR_PTCV (ROW),
                POST_POINT_TYPE_D_VAL = ARR_PTDV (ROW),
                POST_POINT_TYPE_E_VAL = ARR_PTEV (ROW),
                POST_POINT_TYPE_F_VAL = ARR_PTFV (ROW),
                POST_POINT_TYPE_G_VAL = ARR_PTGV (ROW),
                POST_POINT_TYPE_H_VAL = ARR_PTHV (ROW),
                POST_POINT_TYPE_I_VAL = ARR_PTIV (ROW),
                POST_POINT_TYPE_J_VAL = ARR_PTJV (ROW)
          WHERE MEMBER_ID = ARR_MEM_ID (ROW);

      ROW_COUNT := ROW_COUNT + SQL%ROWCOUNT;

      UPDATE SIEBEL_PURGE.PURGE_LOG
         SET PROCESSED_ROWS = ROW_COUNT
       WHERE ROW_ID = LOG_ROW_ID;

      COMMIT;

      EXIT WHEN C1%NOTFOUND;
   END LOOP;

   CLOSE C1;

   UPDATE SIEBEL_PURGE.PURGE_LOG
      SET END_TIME = SYSDATE, PROCESSED_ROWS = ROW_COUNT
    WHERE ROW_ID = LOG_ROW_ID;

   COMMIT;
END;
/

QUIT