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

DECLARE
   V_PURGE_DT         DATE := TO_DATE ('10/01/2012', 'MM/DD/YYYY');
   V_PRODUCT_NAME     VARCHAR2 (100 CHAR) := 'CONSOLIDATED TRANSACTIONS 2015';
   V_PARTITION_NAME   VARCHAR2 (50 CHAR) := 'partion name here';

   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_CONFIG table', SYSDATE
        FROM DUAL;

   COMMIT;

   UPDATE SIEBEL_PURGE.PURGE_CONFIG
      SET ROW_ID =
             (SELECT MAX (ROW_ID) + 1
                FROM SIEBEL_PURGE.PURGE_CONFIG)
    WHERE ROW_ID = 1;

   ROW_COUNT := SQL%ROWCOUNT;

   COMMIT;

   INSERT INTO SIEBEL_PURGE.PURGE_CONFIG (ROW_ID, PURGE_DATE)
        VALUES (1, V_PURGE_DT);

   ROW_COUNT := ROW_COUNT + SQL%ROWCOUNT;

   UPDATE SIEBEL_PURGE.PURGE_CONFIG T1
      SET (PRODUCT_NAME, PROD_ROW_ID, PROD_INT_ID) =
             (SELECT T2.NAME, T2.ROW_ID, T2.INTEGRATION_ID
                FROM SIEBEL.S_PROD_INT T2
               WHERE T2.NAME = V_PRODUCT_NAME)
    WHERE T1.ROW_ID = 1;

   UPDATE SIEBEL_PURGE.PURGE_CONFIG
      SET PARTITION_NAME = V_PARTITION_NAME
    WHERE ROW_ID = 1;

   UPDATE SIEBEL_PURGE.PURGE_CONFIG T1
      SET (NET_ATTRIB_DEFN_ID) =
             (SELECT ROW_ID
                FROM SIEBEL.S_LOY_ATTRDEFN
               WHERE DISPLAY_NAME = 'Net')
    WHERE T1.ROW_ID = 1;

   COMMIT;

   UPDATE SIEBEL_PURGE.PURGE_LOG
      SET END_TIME = SYSDATE, PROCESSED_ROWS = ROW_COUNT
    WHERE ROW_ID = LOG_ROW_ID;

   COMMIT;
END;
/

