I would probably use nested table, varray adds nothing except a limit and takes away a number of options.
Code for varry and nested table is pretty much identical here though except for the declaration.
TYPE list_blank_sections_type IS TABLE OF VARCHAR2(50);
blank_secs list_blank_sections_type := list_blank_sections_type(); -- initialise
FOR i IN 1 .. 10 LOOP
blank_secs(blank_secs.last) := 'SECTION_' || i;
FOR i IN 1 .. 10 LOOP
dbms_output.put_line(i || ' - ' || blank_secs(i));
I appreciate your help but Im not sure that the example above best suits what I already have. It's kind of throwing me off. I will pseudocode what I have as it is too much to copy n paste into here.
BEGIN output (Title of document) output (Purpose of document) loop fetch sectors into csectors sector_id := csector.inspection_sector_id; SELECT count(*) into has_incid FROM inspection_feat_report_view where HIGH_WATER_EVENT_ID = high_water_eventid and INSPECTION_SECTOR_ID = sector_id; if(has_incid != 0) then DO STUFF end if; if(has_incid = 0) then HERE IS WHERE I NEED TO ADD INFORMATION TO MY TABLE OR VARRAY NOTICE I AM STILL IN MY INITIAL LOOP SO WOULD I NEED TO INCREMENT A COUNTER HERE? HOW WOULD THE SYNTAX LOOK TO ADD VALUES TO A TABLE / VARRAY HERE? end if; end loop Now i can print out everything from within my table using another loop
TYPE list_blank_sections_type IS VARRAY(20) of VARCHAR2(50);
Is that correct>?
I personally do not like VARRAYS, or see any compelling reason to use these. Why constrain the number of elements in an array - especially as code and data seldom are static?
Defining an (unconstrained and standard) array:
declare type TStringArray is table of varchar2(100); myArray TStringArray;
Constructing an empty array:
begin myArray := new TStringArrray();
Constructing an array with default values:
begin myArray := new TStringArrray( 'Tom', 'D1ck', 'Harry', 'Sally' );
Constructing an array using SQL:
begin select distinct object_type bulk collect into myArray from user_objects order by 1;
Extending an array:
begin ..array was constructed (and optionally populated) myArray.Extend( 5 ); -- add 5 new array cells (as nulls)
Looping through an array (do not use .First and .Last methods unless you fully realise the consequences of that):
begin ... for i in 1..myArray.Count loop ..do something with myArray(i) end loop;
None of this should be foreign to a programmer that has dealt with structured programming languages, with the exception of using SQL bulk collection to perform (implicit in this case) bulk fetching. And of course the fact that unlike other languages, array offsets start at 1 in PL/SQL and not 0.