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

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, 'Populate PURGE_MEM_REDEEMABLE Part-3/3 After Purge', SYSDATE FROM DUAL;

   COMMIT;

   UPDATE SIEBEL_PURGE.PURGE_MEM_REDEEMABLE
      SET POST_ACRL_SUM = 0
    WHERE POST_ACRL_SUM IS NULL;

   COMMIT;

   UPDATE SIEBEL_PURGE.PURGE_MEM_REDEEMABLE
      SET POST_USED_SUM = 0
    WHERE POST_USED_SUM IS NULL;

   COMMIT;

   UPDATE SIEBEL_PURGE.PURGE_MEM_REDEEMABLE
      SET POST_RDM_SUM = 0
    WHERE POST_RDM_SUM IS NULL;

   COMMIT;

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

   UPDATE /*+ parallel(PURGE_MEM_REDEEMABLE,12) */ SIEBEL_PURGE.PURGE_MEM_REDEEMABLE
      SET TXN_REDMBLE_PTS_POSTPURGE = POST_ACRL_SUM - POST_USED_SUM;

   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