ORA-06531: reference to uninitialized collection Options

codeling Posts: 1085 Points: 4548
Posted: Friday, February 3, 2017 1:09:19 PM

When I try to run the following Oracle PL/SQL block:

I got the error:

ORA-06531: Reference to uninitialized collection
ORA-06512: at line 11


codeling Posts: 1085 Points: 4548
Posted: Friday, February 3, 2017 1:31:53 PM

The nested table variable has to be initialized and extended before you can use it in the assignement statement.

declare

type nested_type is table of number;

v1 nested_type := nested_type();

begin

   for i in 1..10

   loop

      v1.extend;

      v1(i) := i;

   end loop;

   for j in 1..v1.COUNT

   loop

      dbms_output.put_line(v1(j));

   end loop;

end;


codeling Posts: 1085 Points: 4548
Posted: Friday, February 3, 2017 1:35:16 PM

You can also define v1 as a variable of an associative array type, then it will be initialized automatically.

declare

type nested_type is table of number INDEX BY PLS_INTEGER;

v1 nested_type;

begin

   for i in 1..10

   loop

      v1(i) := i;

   end loop;

   for j in 1..v1.COUNT

   loop

      dbms_output.put_line(v1(j));

   end loop;

end;

Users browsing this topic
Guest