--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_MEM_NUM IS TABLE OF SIEBEL_PURGE.PURGE_MEM_INFO.MEM_NUM%TYPE;

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

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

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

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

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

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

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

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

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

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

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

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

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

   ARR_MEM_ID    T_MEM_ID;
   ARR_MEM_NUM   T_MEM_NUM;
   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(P,12) full(P) */ P.MEMBER_ID,
             P.MEM_NUM,
             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_TXN P
             INNER JOIN SIEBEL.S_LOY_MEMBER M ON P.MEMBER_ID = M.ROW_ID
       WHERE P.MEM_NUM IS NOT NULL;
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 Before Purge', SYSDATE
        FROM DUAL;

   COMMIT;

   OPEN C1;

   LOOP
      FETCH C1
         BULK COLLECT INTO ARR_MEM_ID,
              ARR_MEM_NUM,
              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
         INSERT INTO SIEBEL_PURGE.PURGE_MEM_INFO (MEMBER_ID,
                                                  MEM_NUM,
                                                  PRE_LAST_TXN_PROCED_DT,
                                                  PRE_LAST_ACCR_TXN_DT,
                                                  PRE_LAST_RDM_TXN_DT,
                                                  PRE_POINT_TYPE_A_VAL,
                                                  PRE_POINT_TYPE_B_VAL,
                                                  PRE_POINT_TYPE_C_VAL,
                                                  PRE_POINT_TYPE_D_VAL,
                                                  PRE_POINT_TYPE_E_VAL,
                                                  PRE_POINT_TYPE_F_VAL,
                                                  PRE_POINT_TYPE_G_VAL,
                                                  PRE_POINT_TYPE_H_VAL,
                                                  PRE_POINT_TYPE_I_VAL,
                                                  PRE_POINT_TYPE_J_VAL)
              VALUES (ARR_MEM_ID (ROW),
                      ARR_MEM_NUM (ROW),
                      ARR_LTPD (ROW),
                      ARR_LATD (ROW),
                      ARR_LRTD (ROW),
                      ARR_PTAV (ROW),
                      ARR_PTBV (ROW),
                      ARR_PTCV (ROW),
                      ARR_PTDV (ROW),
                      ARR_PTEV (ROW),
                      ARR_PTFV (ROW),
                      ARR_PTGV (ROW),
                      ARR_PTHV (ROW),
                      ARR_PTIV (ROW),
                      ARR_PTJV (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