-1

I am trying to update records in bulk. After running for all update I need to call a procedure and send all successfully updated id's as one of the parameter to the procedure.All those id's for which update was not performed due to some error should not be passed to the procedure. My code is below.

DECLARE
    type emp_type is TABLE OF number;
    EMP_ID EMP_TYPE;
    lv_select varchar2(4000):='select employee_id from EMPLOYEE where dept_NO=1';
BEGIN
    EXECUTE IMMEDIATE lv_select  BULK COLLECT INTO emp_id ;

    FORALL INDX IN 1 ..emp_id.COUNT SAVE EXCEPTIONS  
    UPDATE emp Set salary=salary+1000 
    where employee_id=emp_id(INDX);

    PROC (
    PAR1=>'abc',
    par2=>emp_id(INDX),
    par3=>'xyz'
    );
EXCEPTION
    WHEN OTHERS 
    THEN
    FOR J IN 1 .. SQL%BULK_EXCEPTIONS.COUNT
    LOOP
        DBMS_OUTPUT.PUT_LINE(SQLERRM (-SQL%BULK_EXCEPTIONS (j).ERROR_CODE));
    END LOOP;   
END;
Gurwinder Singh
  • 35,652
  • 5
  • 39
  • 62
PTK
  • 161
  • 14

1 Answers1

0

This can be simply achieved by using simple for LOOP. Hope below snoippet helps.

  DECLARE
  type emp_type
  IS
    TABLE OF NUMBER;
    EMP_ID EMP_TYPE;
    lv_select VARCHAR2(4000):='select employee_id from EMPLOYEE where dept_NO=1';
  BEGIN
    EXECUTE IMMEDIATE lv_select BULK COLLECT INTO emp_id ;
    FOR indx IN emp_id.first.emp_id.last
    LOOP
      BEGIN
        UPDATE emp SET salary=salary+1000 WHERE employee_id=emp_id(indx);
        PROC ( PAR1=>'abc', par2=>emp_id(INDX), par3=>'xyz' );
      EXCEPTION
      WHEN OTHERS THEN
        dbms_output.put_line(emp_id(indx)|| 'Failed due to '||sqlerrm||'-'||SQLCODE);
      END;
    end loop;
  END;
Avrajit Roy
  • 2,977
  • 1
  • 9
  • 22