Thursday, 14 August 2014

cursor with insert statement in pl/sql

/*
REM +======================================================================+
REM |                               |
REM |                   Bangalore,India                                    |
REM |                All rights reserved.                                  |
REM +======================================================================+ */
/*================================================================================================================*/
/*
             DESCRIPTION:
        Deliverable Name:       Data_Fix_210895.sql
             Description:       This ticket has been created for WR#210895 Fix
              Created By:    
       
  USAGE:        
        Run this script from the APPS schema.

  CHANGE HISTORY:
 
             Date                  Name                                           Description
         ----------         --------------------------------------       ---------------------------------
        14/Aug/2014             sathish                                      Created
*/
SET SERVEROUTPUT ON

DECLARE
CURSOR c1_itm IS SELECT * FROM apps.mtl_system_items where organization_id = 5204
AND trim(upper(attribute6))= 'SERVICES'
AND attribute7=1651;

CURSOR c2_org IS SELECT * FROM apps.org_organization_definitions WHERE operating_unit IN (5758, 6359, 5203);

l_count NUMBER := 0;
l_ignore VARCHAR2(10) := 'N';
l_segment1 VARCHAR2(240);

BEGIN
dbms_output.enable(1000000);
FOR r1_itm IN c1_itm LOOP
    FOR r2_org IN c2_org LOOP
    l_ignore := 'N';
    BEGIN
        SELECT segment1
        INTO l_segment1
        FROM mtl_system_items
        WHERE segment1 = r1_itm.segment1
        AND organization_id = r2_org.organization_id;
    EXCEPTION
        WHEN NO_DATA_FOUND THEN
            l_ignore := 'Y';
        WHEN OTHERS THEN
            l_ignore := 'N';
    END;
DBMS_OUTPUT.PUT_LINE('Item Name - Org: ' || r1_itm.segment1 || ' - '|| r2_org.organization_code);
IF l_ignore = 'N' THEN
INSERT INTO mtl_system_items_interface
   (PROCESS_FLAG
   ,SEGMENT1
   ,DESCRIPTION
   ,attribute7
   ,SET_PROCESS_ID
   ,ORGANIZATION_ID
   ,TRANSACTION_TYPE )
   VALUES
   (1
   ,r1_itm.segment1
   ,r1_itm.description
   ,1713
   ,11142999
   ,r2_org.organization_id
   ,'UPDATE');
l_count := l_count + SQL%ROWCOUNT;
END IF;
    END LOOP;
END LOOP;
DBMS_OUTPUT.PUT_LINE('Total Records inserted : ' || l_count);

COMMIT;

EXCEPTION
WHEN OTHERS THEN
  ROLLBACK;
  DBMS_OUTPUT.PUT_LINE('Error when inserting into MSII ' || SQLERRM);
END;
/

No comments:

Post a Comment