Ora-6502数值或值错误,将空值插入到具有NOT NULL列的表中

时间:2014-02-11 19:47:46

标签: oracle plsql ora-06502

我正在使用以下BULK COMMIT proc来从TABLESOURCETABLE1插入记录。 COLUMN1上的TABLE1定义为NOT NULL

有时我的TABLE_SOURCE返回零行,在这种情况下,我的PROC失败并出现错误:

  

Ora - 6502数字或值错误

我尝试在异常中使用NO_DATA_FOUND忽略错误,但它也不起作用。

你能告诉我如何解决这个问题吗?

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;

1 个答案:

答案 0 :(得分:1)

由于

而出现问题
v_Last_Record := a_Fetch_Set.LAST;

  FOR i IN 1 .. v_Last_Record

如果您的光标没有行,则bulk collect不会填充集合,而a_Fetch_Set为空。在这种情况下,LAST返回NULL。你应该检查一下或使用a_Fetch_Set.COUNT。