--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, 'Update PURGE tables'' UPDATED_FLG', SYSDATE FROM DUAL;

   COMMIT;

   UPDATE (SELECT /*+ parallel(t,5) */ T.MEMBER_ID T_MI,
                  T.UPDATED_FLG T_UF,
                  R.MEMBER_ID R_MI,
                  R.UPDATED_FLG R_UF
             FROM SIEBEL_PURGE.PURGE_MEM_TXN T
                  INNER JOIN SIEBEL_PURGE.PURGE_MEM_REDEEMABLE R
                     ON T.MEMBER_ID = R.MEMBER_ID
            WHERE T.UPDATED_FLG = 'T')
      SET R_UF = 'Y'
    WHERE T_MI = R_MI;

   COMMIT;

   UPDATE (SELECT /*+ parallel(t,5) */ T.MEMBER_ID T_MI,
                  T.UPDATED_FLG T_UF,
                  I.MEMBER_ID I_MI,
                  I.UPDATED_FLG I_UF
             FROM SIEBEL_PURGE.PURGE_MEM_TXN T
                  INNER JOIN SIEBEL_PURGE.PURGE_MEM_INFO I
                     ON T.MEMBER_ID = I.MEMBER_ID
            WHERE T.UPDATED_FLG = 'T')
      SET I_UF = 'Y'
    WHERE T_MI = I_MI;

   COMMIT;

   UPDATE SIEBEL_PURGE.PURGE_MEM_TXN
      SET UPDATED_FLG = NULL
    WHERE UPDATED_FLG IS NOT NULL;

   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