I have a situation where I want to have a collection, actually in this case it is a nest collection (Associative array) that I would like to index by varchar so that I can access it by a field element instead of iterating to find a value. The only thing is I don't want to run into the NO_DATA_FOUND error when I try to access a position that does not exist.
So is there a way that I can dynamically take a record type that has elements such as
TYPE sample_emp_rec IS RECORD (name VARCHAR2(30),
dob DATE,
position VARCHAR2(30));
THEN initialize a collection:
TYPE sample_emp_elem_typ IS TABLE OF VARCHAR2(30) INDEX BY VARCHAR2(2);
sample_emp_elem_tbl sample_emp_elem_typ;
FOR element_name IN sample_emp_rec%ROWTYPE LOOP
sample_emp_elem_tbl(element_name) := NULL;
END LOOP;
Then I assign some values
sample_emp_elem_tbl(name) := 'John';
sample_emp_elem_tbl(position) 'Boss';
Then I try to access the non-existent element
CASE WHEN sample_emp_elem_tbl(dob) = to(date('01/01/2018','mm/dd/yyyy')
THEN 'do something'
END CASE;
If I hadn't initialized that position, I would get a no data found but in this case I would just get null.
Am I dreaming that something like this is possible. Is there a better way to do this?
One thing I could do is always check for the existence but that didn't seem good like
CASE WHEN sample_emp_elem_tbl(dob).EXISTS AND sample_emp_elem_tbl(dob)
= to(date('01/01/2018','mm/dd/yyyy')
THEN 'do something'
END CASE;
That being said, I would still like to know dynamically what elements are in the current record. I will potentially have dozens of record types to work with.