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

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

   TYPE T_MEM_NUM IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.MEM_NUM%TYPE;

   TYPE T_PER_UID IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.PERSON_UID%TYPE;

   ARR_MEM_ID    T_MEM_ID;
   ARR_MEM_NUM   T_MEM_NUM;
   ARR_PER_UID   T_PER_UID;
   LOG_ROW_ID    NUMBER := 0;
   ROW_COUNT     NUMBER := 0;

   CURSOR C1
   IS
      SELECT /*+ index_ffs(P,PURGE_MEM_TXN_P1) parallel_index(P,PURGE_MEM_TXN_P1,12) */ M.ROW_ID, M.MEM_NUM, C.PERSON_UID
        FROM SIEBEL_PURGE.PURGE_MEM_TXN P
             INNER JOIN SIEBEL.S_LOY_MEMBER M ON P.MEMBER_ID = M.ROW_ID
             INNER JOIN SIEBEL.S_CONTACT C ON C.ROW_ID = M.PR_CON_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_TXN Part-2/6', SYSDATE FROM DUAL;

   COMMIT;

   OPEN C1;

   LOOP
      FETCH C1
         BULK COLLECT INTO ARR_MEM_ID, ARR_MEM_NUM, ARR_PER_UID
         --LIMIT 10000;
		 LIMIT 50;

      FORALL ROW IN ARR_MEM_ID.FIRST .. ARR_MEM_ID.LAST
         UPDATE SIEBEL_PURGE.PURGE_MEM_TXN
            SET MEM_NUM = ARR_MEM_NUM (ROW), PERSON_UID = ARR_PER_UID (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