--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_SUV IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.SUMMARY_USED_VALUE%TYPE;

   TYPE T_IN IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.ITEM_NUM%TYPE;

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

   ARR_SNP      T_SNP;
   ARR_SUV      T_SUV;
   ARR_IN       T_IN;
   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.SUMMARY_USED_VALUE,
             A.ITEM_NUM,
             A.MEM_NUM
        FROM SIEBEL_PURGE.PURGE_MEM_TXN A
       WHERE     A.PURGE_TXN_COUNT > 0
             AND A.ITEM_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_ACRLIT', SYSDATE FROM DUAL;

   COMMIT;

   OPEN C1;

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

      FORALL ROW IN ARR_IN.FIRST .. ARR_IN.LAST
         UPDATE SIEBEL.EIM_LOY_ACRLIT
            SET ACCRUALED_VALUE = ARR_SNP (ROW),
                USED_VALUE = ARR_SUV (ROW),
                MEMBER_MEM_NUM = ARR_MN (ROW)
          WHERE ITEM_NUM = ARR_IN (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