I'd like to create an in-memory array variable that can be used in my PL/SQL code. I can't find any collections in Oracle PL/SQL that uses pure memory, they all seem to be associated with tables. I'm looking to do something like this in my PL/SQL (C# syntax):
string[] arrayvalues = new string[3] {"Matt", "Joanne", "Robert"};
Edit: Oracle: 9i
You can also use an
oracle defined collection
I would use in-memory array. But with the
.COUNT
improvement suggested by uziberia:Another solution would be to use a Hashmap like @Jchomel did here.
NB:
With Oracle 12c you can even query arrays directly now!
You can use VARRAY for a fixed-size array:
Or TABLE for an unbounded array:
The word "table" here has nothing to do with database tables, confusingly. Both methods create in-memory arrays.
With either of these you need to both initialise and extend the collection before adding elements:
The first index is 1 not 0.
You could just declare a DBMS_SQL.VARCHAR2_TABLE to hold an in-memory variable length array indexed by a BINARY_INTEGER:
You could use an associative array (used to be called PL/SQL tables) as they are an in-memory array.
The associative array can hold any make up of record types.
Hope it helps, Ollie.
Another solution is to use an Oracle Collection as a Hashmap: