Question

I'm using a below BULK COMMIT proc to insert records from TABLESOURCE to TABLE1. COLUMN1 on TABLE1 is defined as NOT NULL.

Sometimes my TABLE_SOURCE returns zero rows, and in that case my PROC is failing with error:

Ora - 6502 Numeric or value error

I tried to ignore the error using the NO_DATA_FOUND in exception but it didn't work either.

Could you please let me know how to resolve this?

create or replace PROCEDURE  PROC1  AS
    v_ErrNum            NUMBER (20);
    v_ErrMsg            VARCHAR2 (270);
    v_Last_Record       PLS_INTEGER;

   CURSOR c_Get_Record
   IS
      SELECT Column1, Column2, Column3
        FROM Table1;
    TYPE Target_Rec IS TABLE OF Table1%ROWTYPE
      INDEX BY PLS_INTEGER;
        a_Target_Rec        Target_Rec;
    TYPE FetchSet IS TABLE OF c_Get_Record%ROWTYPE;
        a_Fetch_Set         FetchSet;
    TYPE cursor_var_type IS REF CURSOR;
        CV                  cursor_var_type;
BEGIN
    OPEN CV FOR
        Select  Column1, Column2, Column3 from TABLE_SOURCE where Column1 is NOT NULL;
    FETCH CV 
   BULK COLLECT INTO a_Fetch_Set;
    CLOSE CV;
        v_Last_Record := a_Fetch_Set.LAST;

  FOR i IN 1 .. v_Last_Record
   LOOP
      a_Target_Rec (i).Column1 := a_Fetch_Set (i).Column1;
      a_Target_Rec (i).Column2 := a_Fetch_Set (i).Column2;
      a_Target_Rec (i).Column3 := a_Fetch_Set (i).Column3;

  END LOOP;
        v_Last_Record := 0;
        v_Last_Record := a_Target_Rec.LAST;

   FORALL j IN 1 .. v_Last_Record
      INSERT INTO TABLE1
           VALUES a_Target_Rec (j);

   COMMIT;
     -- *** ** Error Handling ** ***

EXCEPTION
  WHEN OTHERS THEN
        v_ErrNum := SQLCODE ;
        v_ErrMsg := SUBSTR( SQLERRM, 0, 250) ;
        RAISE_APPLICATION_ERROR( -20067 , 'ERROR WHILE LOADING TABLE1  Error Msg: ' || dbms_utility.format_error_stack|| dbms_utility.format_error_backtrace) ;
        ROLLBACK ;
END PROC1;
Was it helpful?

Solution

You get a problem due to

v_Last_Record := a_Fetch_Set.LAST;

  FOR i IN 1 .. v_Last_Record

In case your cursor does not have rows bulk collect does not fill collection and a_Fetch_Set is empty. In this case LAST returns NULL. You should check this or use a_Fetch_Set.COUNT.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top