Monday, 6 February 2017

ORA-06531: Reference to uninitialized collection

Scenario 1: With VARRAY

In VARRAY, We must initialize before storing data otherwise you would face below error.

























Initialize Varray as below.

Here we are initializing Varray and Extending it to allocate space.



















Scenario 2: With Nested Table


Same as VARRAY, In Nested table also we must initialize before storing data  otherwise you would face below error.

DECLARE
  TYPE TY_TB_V_PEOPLE_DET IS TABLE OF PEOPLE_DET%ROWTYPE;
  V_TY_TB_V_PEOPLE_DET TY_TB_V_PEOPLE_DET;
  l_error_count        NUMBER;
  IDX                  INTEGER := 0;
  CURSOR CUR_People_Det IS
    SELECT * FROM People_Det;
  bulk_dml_exp EXCEPTION;
  PRAGMA EXCEPTION_INIT(bulk_dml_exp, -24123);
BEGIN
  FOR K IN CUR_People_Det LOOP
    IDX := IDX + 1;
    V_TY_TB_V_PEOPLE_DET(IDX) .PAN := K.PAN;
    V_TY_TB_V_PEOPLE_DET(IDX) .NAME := K.NAME;
  END LOOP;
DBMS_OUTPUT.PUT_LINE('Count ' || V_TY_TB_V_PEOPLE_DET.COUNT);
END;

ORA-06531: Reference to uninitialized collection

ORA-06512: at line 14

Fix:


Initialize Nested table as below.

Here we are initializing Nested table and Extending it to allocate space.

DECLARE
  TYPE TY_TB_V_PEOPLE_DET IS TABLE OF PEOPLE_DET%ROWTYPE;
  V_TY_TB_V_PEOPLE_DET TY_TB_V_PEOPLE_DET;
  l_error_count        NUMBER;
  IDX                  INTEGER := 0;
  CURSOR CUR_People_Det IS
    SELECT * FROM People_Det;
  bulk_dml_exp EXCEPTION;
  PRAGMA EXCEPTION_INIT(bulk_dml_exp, -24123);
BEGIN
--Initializing Nested table
V_TY_TB_V_PEOPLE_DET := TY_TB_V_PEOPLE_DET();
  FOR K IN CUR_People_Det LOOP
  --Allocating space Using Extend Method In loop so it will allocate until loop executes
  V_TY_TB_V_PEOPLE_DET.EXTEND;
    IDX := IDX + 1;
    V_TY_TB_V_PEOPLE_DET(IDX) .PAN := K.PAN;
    V_TY_TB_V_PEOPLE_DET(IDX) .NAME := K.NAME;
  END LOOP;
DBMS_OUTPUT.PUT_LINE(' Count ' || V_TY_TB_V_PEOPLE_DET.COUNT);

END;


No comments:

Post a Comment