--Author: Venkata Potluri
--Date: Oct 20, 2015

DECLARE
   TYPE T_ROW_ID IS TABLE OF SIEBEL_PURGE.TEMP_TXN_10212015.ROW_ID%TYPE;

   TYPE T_MEM_ID IS TABLE OF SIEBEL_PURGE.TEMP_TXN_10212015.MEMBER_ID%TYPE;

   TYPE T_CREATED IS TABLE OF SIEBEL_PURGE.TEMP_TXN_10212015.CREATED%TYPE;

   TYPE T_PROC_DT IS TABLE OF SIEBEL_PURGE.TEMP_TXN_10212015.PROCESS_DT%TYPE;

   TYPE T_NUM_POINTS
      IS TABLE OF SIEBEL_PURGE.TEMP_TXN_10212015.NUM_POINTS%TYPE;

   ARR_ROW_ID       T_ROW_ID;
   ARR_MEM_ID       T_MEM_ID;
   ARR_CREATED      T_CREATED;
   ARR_PROC_DT      T_PROC_DT;
   ARR_NUM_POINTS   T_NUM_POINTS;

   CURSOR C1
   IS
      SELECT ROW_ID,
             MEMBER_ID,
             CREATED,
             PROCESS_DT,
             NUM_POINTS
        FROM SIEBEL.S_LOY_TXN T
       WHERE     TYPE_CD = 'Accrual'
             AND SUB_TYPE_CD = 'Cancellation'
             AND PROCESS_DT > TO_DATE ('10/18/2015', 'mm/dd/yyyy');
BEGIN
   OPEN C1;

   LOOP
      FETCH C1
         BULK COLLECT INTO ARR_ROW_ID,
              ARR_MEM_ID,
              ARR_CREATED,
              ARR_PROC_DT,
              ARR_NUM_POINTS
         LIMIT 500;

      FORALL ROW IN ARR_ROW_ID.FIRST .. ARR_ROW_ID.LAST
         INSERT INTO SIEBEL_PURGE.TEMP_TXN_10212015 (ROW_ID,
                                                     MEMBER_ID,
                                                     CREATED,
                                                     PROCESS_DT,
                                                     NUM_POINTS)
              VALUES (ARR_ROW_ID (ROW),
                      ARR_MEM_ID (ROW),
                      ARR_CREATED (ROW),
                      ARR_PROC_DT (ROW),
                      ARR_NUM_POINTS (ROW));

      COMMIT;

      EXIT WHEN C1%NOTFOUND;
   END LOOP;

   CLOSE C1;
END;
/

QUIT