--Author: Venkata Potluri
--Date: April 08, 2015
--Let's see if this requires any tuning

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_INFO Part-2/2', SYSDATE FROM DUAL;

   COMMIT;

   UPDATE (SELECT P.MEMBER_ID P_MI,
                  P.MEM_NUM P_MN,
                  P.PRE_LAST_TXN_PROCED_DT P_1,
                  P.PRE_LAST_ACCR_TXN_DT P_2,
                  P.PRE_LAST_RDM_TXN_DT P_3,
                  P.PRE_POINT_TYPE_A_VAL P_A,
                  P.PRE_POINT_TYPE_B_VAL P_B,
                  P.PRE_POINT_TYPE_C_VAL P_C,
                  P.PRE_POINT_TYPE_D_VAL P_D,
                  P.PRE_POINT_TYPE_E_VAL P_E,
                  P.PRE_POINT_TYPE_F_VAL P_F,
                  P.PRE_POINT_TYPE_G_VAL P_G,
                  P.PRE_POINT_TYPE_H_VAL P_H,
                  P.PRE_POINT_TYPE_I_VAL P_I,
                  P.PRE_POINT_TYPE_J_VAL P_J,
                  P.UPDATED_FLG P_UF,
                  M.MEM_NUM M_MN,
                  M.LAST_TXN_PROCED_DT M_1,
                  M.LAST_ACCR_TXN_DT M_2,
                  M.LAST_RDM_TXN_DT M_3,
                  M.POINT_TYPE_A_VAL M_A,
                  M.POINT_TYPE_B_VAL M_B,
                  M.POINT_TYPE_C_VAL M_C,
                  M.POINT_TYPE_D_VAL M_D,
                  M.POINT_TYPE_E_VAL M_E,
                  M.POINT_TYPE_F_VAL M_F,
                  M.POINT_TYPE_G_VAL M_G,
                  M.POINT_TYPE_H_VAL M_H,
                  M.POINT_TYPE_I_VAL M_I,
                  M.POINT_TYPE_J_VAL M_J,
                  M.ROW_ID M_RI
             FROM SIEBEL_PURGE.PURGE_MEM_INFO P
                  INNER JOIN SIEBEL.S_LOY_MEMBER M ON P.MEMBER_ID = M.ROW_ID
            WHERE P.UPDATED_FLG = 'Y')
      SET P_MN = M_MN,
          P_1 = M_1,
          P_2 = M_2,
          P_3 = M_3,
          P_A = M_A,
          P_B = M_B,
          P_C = M_C,
          P_D = M_D,
          P_E = M_E,
          P_F = M_F,
          P_G = M_G,
          P_H = M_H,
          P_I = M_I,
          P_J = M_J,
          P_UF = NULL
    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