--Author: Venkata Potluri
--Date: July 17, 2015

DECLARE
   TYPE T_SNP IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.SUMMARY_NUM_POINTS%TYPE;

   TYPE T_TN IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.TXN_NUM%TYPE;

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

   ARR_SNP      T_SNP;
   ARR_TN       T_TN;
   ARR_MN       T_MN;

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

   CURSOR C1
   IS
      SELECT /*+ parallel(A,5) */
            A.SUMMARY_NUM_POINTS, A.TXN_NUM, A.MEM_NUM
        FROM SIEBEL_PURGE.PURGE_MEM_TXN A
       WHERE     A.PURGE_TXN_COUNT > 0
             AND A.TXN_NUM IS NOT NULL
             AND UPDATED_FLG = 'T';
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, 'Update EIM_LOY_TXN', SYSDATE FROM DUAL;

   COMMIT;

   OPEN C1;

   LOOP
      --FETCH C1 BULK COLLECT INTO ARR_SNP, ARR_TN, ARR_MN LIMIT 10000;
	  FETCH C1 BULK COLLECT INTO ARR_SNP, ARR_TN, ARR_MN LIMIT 50;

      FORALL ROW IN ARR_TN.FIRST .. ARR_TN.LAST
         UPDATE SIEBEL.EIM_LOY_TXN
            SET NUM_POINTS = ARR_SNP (ROW), MEMBER_MEM_NUM = ARR_MN (ROW)
          WHERE TXN_NUM = ARR_TN (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;

   COMMIT;

   CLOSE C1;

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

   COMMIT;
END;
/

QUIT