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

DECLARE
   TYPE T_PID IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.PERSON_UID%TYPE;

   TYPE T_AS IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.ACRL_SUM%TYPE;

   TYPE T_RS IS TABLE OF SIEBEL_PURGE.PURGE_MEM_TXN.RDM_SUM%TYPE;

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

   ARR_PID          T_PID;
   ARR_AS           T_AS;
   ARR_RS           T_RS;
   ARR_MN           T_MN;

   V_PRODUCT_NAME   VARCHAR2 (100 CHAR);
   LOG_ROW_ID       NUMBER := 0;
   ROW_COUNT        NUMBER := 0;

   CURSOR C1
   IS
      SELECT /*+ parallel(A,5) */
            A.PERSON_UID,
             A.MEM_NUM,
             A.ACRL_SUM,
             A.RDM_SUM
        FROM SIEBEL_PURGE.PURGE_MEM_TXN A
       WHERE     A.PURGE_TXN_COUNT > 0
             AND A.TXN_NUM IS NOT NULL
             AND A.PERSON_UID 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_CON_DTL Notes', SYSDATE FROM DUAL;

   COMMIT;

   SELECT PRODUCT_NAME
     INTO V_PRODUCT_NAME
     FROM SIEBEL_PURGE.PURGE_CONFIG
    WHERE ROW_ID = 1;

   OPEN C1;

   LOOP
      FETCH C1
         BULK COLLECT INTO ARR_PID,
              ARR_MN,
              ARR_AS,
              ARR_RS
         --LIMIT 10000;
		 LIMIT 50;

      FORALL ROW IN ARR_PID.FIRST .. ARR_PID.LAST
         UPDATE /*+ index(EIM_CON_DTL,EIM_CON_DTL_T08) */
               SIEBEL.EIM_CON_DTL
            SET NOTE_NOTE =
                      'For Member#: '
                   || ARR_MN (ROW)
                   || ' '
                   || V_PRODUCT_NAME
                   || ' had '
                   || ARR_AS (ROW)
                   || ' accrued value and '
                   || ARR_RS (ROW)
                   || ' redemption value'
          WHERE     CON_PERSON_UID = ARR_PID (ROW)
                AND IF_ROW_BATCH_NUM >= 7001
                AND IF_ROW_BATCH_NUM <= 8000;

      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