I've tried to Map a table with associative arrays , but i can't figure out how to initialize it
here is an example :
TYPE RecType IS RECORD
(
value1 NUMBER,
value2 NUMBER,
value3 NUMBER
);
TYPE TblType IS TABLE OF RecType INDEX BY PLS_INTEGER;
TYPE TblOfTblType IS TABLE OF TblType INDEX BY PLS_INTEGER;
matrix TblOfTblType;
Now when i tried to initialize the matrix like this :
FOR i IN matrix.FIRST .. matrix.LAST LOOP
FOR j IN matrix (i).FIRST .. matrix (i).LAST LOOP
matrix(i)(j) := NULL;
END LOOP;
END LOOP;
It doesn't work !I also tried
matrix := TblOfTblType()();
it shows the following error :
PLS-00363 expression 'Matrix' cannot be used as an assignment target .
You do not need to use an associative array - a collection will work.
Although I agree with @MTO's approach, the error from your loop approach is because you are trying to refer to
FIRST
andLAST
on an empty table, and they both evaluate to null at that point. You are effectively trying to do:which gets the same
ORA-06502: PL/SQL: numeric or value error
as your originalFOR
loop.You haven't defined anywhere what the dimensions of the matrix should be. You need to do that somewhere to be able to 'initialise' it, e.g. with fixed values to match MTO's and your type declarations:
To populate with non-null values you woudl need a separate record variable that you populate and then assign tot he matrix position:
or you could target specific matrix elements without a loop if you're getting those form somewhere else. And you can print it out the same way MTO showed too, or with
FIRST
andLAST
, which are now valid: