I'm pretty new to PL/SQL and currently I have the need for a specific functionality, which I suspect to not being doable with PL/SQL. I am bound to using PL/SQL, so hints on other programming ('query') languages will unfortunately be of no use to me. Therefore I'd like to ask, whether it is possible to create instances of user-defined types in PL/SQL during program flow and not only within the DECLARE block, and in case it is possible, I'd like to know how to do it.
Scenario:
I want to create a list of lists like...
TYPE SIMPLE_LIST IS TABLE OF VARCHAR2(30) INDEX BY VARCHAR(30);
TYPE LIST_OF_LISTS IS TABLE OF SIMPLE_LIST INDEX BY VARCHAR2(30);
The creation of the type works without any problems.
In my program, I have a function, which has the need to declare such a LIST_OF_LISTS and fill it dynamically.
Therefore the simplified code sample shall look like...
FUNCTION foo(...) RETURN ...
AS
ll LIST_OF_LISTS;
sl SIMPLE_LIST;
...
BEGIN
LOOP -- iterate over something
...
sl := new SIMPLE_LIST; -- this surely doesn't work
sl('key1') := ...;
sl('key2') := ...;
sl('key3') := ...;
...
ll('iter_key') := sl;
END LOOP;
RETURN ll;
END foo;
I want/need to use such a list of lists, because I cannot determine the length of each list (and also not of the list of lists) before runtime.
As one can already tell, I'm looking for an OO-like functionality to create an instance of a type with something like a 'new'-operator, right in the middle of the program flow, to fill the list of lists dynamically. The line with the 'new'-operator is just a hint on what I want to accomplish, as I am aware that this is not the way to encompass the described task.
Can anyone give me a hint on how I might be able to implement the described scenario using PL/SQL?
EDIT
As it might be of interest, here's a little more background information on the actual task I am trying to achieve. In a nutshell, the function 'foo' shall extract a few items from an xml document and return the result packed in a data structure for later processing, which is why I ended up with the approach of using such a list of lists. The function foo receives an xml document (XMLTYPE), as well as a list of items to be searched for during the parsing of the document. While the document is being parsed, using the DBMS_XMLDOM-package, the list is filled with key and value of each XML tag that matches one of the elements to be searched for. Due to the fact, that the XML tags may not be unique across the whole document but occur multiple times, I came up with the idea to use the defined SIMPLE_LIST to store the values of every single occurrence of XML tags/elements (keys) to be searched for. So the 'key'/'index' of LIST_OF_LISTS shall eventually contain the name of the XML tag/element, while the SIMPLE_LIST shall contain all values of any occurrence of the corresponding XML tag/element, packed together in one list. The amount of entries within the list to be returned will be rather small (definitely not more than 100 entries), therefore I assume that the use of actual tables or nested tables might be overkill in this case.
Thanks in advance.
Chris
EDIT²
I tested the answers of both Boneist and Mr. Łukasiewicz, and I can confirm that they both worked when applied to my scenario. I accepted the latter, due to the fact that it is the most compact answer.
Thanks again for solving my problem.
Cheers, Chris
LIST_OF_LISTS? What is the purpose of using it? You could first explain your requirement. Seems like XY problem. You are stuck with problem X, but end up explaining problem Y which is your interpretation of the actual problem.