--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_MEM_TXN Part-1/6', SYSDATE FROM DUAL;

   COMMIT;

   UPDATE (SELECT P.MEMBER_ID P_MI,
                  P.MEM_NUM P_MN,
                  P.PERSON_UID P_PCI,
                  M.MEM_NUM M_MN,
                  C.PERSON_UID M_PCI,
                  M.ROW_ID M_RI
             FROM SIEBEL_PURGE.PURGE_MEM_TXN P
                  INNER JOIN SIEBEL.S_LOY_MEMBER M ON P.MEMBER_ID = M.ROW_ID
                  INNER JOIN SIEBEL.S_CONTACT C ON C.ROW_ID = M.PR_CON_ID
            WHERE UPDATED_FLG = 'M')
      SET P_MN = M_MN, P_PCI = M_PCI
    WHERE P_MI = M_RI;

   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