--Author: Venkata Potluri
--Date: April 04, 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_TXN Part-5/6', SYSDATE FROM DUAL;

   COMMIT;

   UPDATE SIEBEL_PURGE.PURGE_MEM_TXN
      SET ACRL_SUM = 0
    WHERE ACRL_SUM IS NULL;

   COMMIT;

   UPDATE SIEBEL_PURGE.PURGE_MEM_TXN
      SET USED_SUM = 0
    WHERE USED_SUM IS NULL;

   COMMIT;

   UPDATE SIEBEL_PURGE.PURGE_MEM_TXN
      SET RDM_SUM = 0
    WHERE RDM_SUM IS NULL;

   COMMIT;

   -- if performance is not good, use plsql with bulk update
   UPDATE SIEBEL_PURGE.PURGE_MEM_TXN
      SET SUMMARY_NUM_POINTS = ACRL_SUM - RDM_SUM,
          SUMMARY_USED_VALUE = USED_SUM - RDM_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