--Author: Venkata Potluri
--Date: April 08, 2015
--TAble is only 500MB, we can add the following index to the updates if necessary
/*+ parallel(PURGE_MEM_REDEEMABLE,5) */

DECLARE
   LOG_ROW_ID   NUMBER := 0;
   ROW_COUNT    NUMBER := 0;
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 PURGE_MEM_REDEEMABLE Part-5/5', SYSDATE FROM DUAL;

   COMMIT;

   UPDATE /*+ parallel(PURGE_MEM_REDEEMABLE,5) */ SIEBEL_PURGE.PURGE_MEM_REDEEMABLE
      SET PRE_ACRL_SUM = 0
    WHERE PRE_ACRL_SUM IS NULL AND UPDATED_FLG = 'Y';

   COMMIT;

   UPDATE /*+ parallel(PURGE_MEM_REDEEMABLE,5) */ SIEBEL_PURGE.PURGE_MEM_REDEEMABLE
      SET PRE_USED_SUM = 0
    WHERE PRE_USED_SUM IS NULL AND UPDATED_FLG = 'Y';

   COMMIT;

   UPDATE /*+ parallel(PURGE_MEM_REDEEMABLE,5) */ SIEBEL_PURGE.PURGE_MEM_REDEEMABLE
      SET PRE_RDM_SUM = 0
    WHERE PRE_RDM_SUM IS NULL AND UPDATED_FLG = 'Y';

   COMMIT;

   -- if performance is not good, use plsql with bulk update

   UPDATE /*+ parallel(PURGE_MEM_REDEEMABLE,5) */ SIEBEL_PURGE.PURGE_MEM_REDEEMABLE
      SET TXN_REDMBLE_PTS_PREPURGE = PRE_ACRL_SUM - PRE_USED_SUM,
          UPDATED_FLG = NULL
    WHERE UPDATED_FLG = 'Y';

   ROW_COUNT := SQL%ROWCOUNT;
   COMMIT;

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

   COMMIT;
END;
/

QUIT