Skip to content
Advertisement

Not able to create a matrix in PL/SQL using nested tables

I was trying to create a matrix in PL/SQL using nested tables as such:

  • firstly, I create a table type that is able to store varchars (that in my case is name lista_principala)
  • secondly, using the previously declare table type lista_principala I try to create a table type that contains items of type lista_principala. I don’t think that I’m wrong about my logic, but my PL/SQL implementation looks something like this:

And the problem that I have is that when I try to run this script I get the following errors:

Error report – ORA-06531: Reference to uninitialized collection ORA-06512: at line 12

Advertisement

Answer

You use lista.extend(20); to create a list with 20 items and these will all be initialised to NULL.

Then you set the values for the first 3 elements of the collection.

Then you loop through all 20 items and try to loop through the sub-list in each element; however, after the first 3, there is no sub-list contained in the element as the element is NULL.

Either:

  • Just lista.EXTEND(3); as you only want 3 elements to the array; or
  • Add a check if the list element is NULL and then skip looping through the sub-list if it is.

The second option can be implemented as:

db<>fiddle here

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement