Aug 232013
 

In PL/SQL, in order to iterate over a list of values (numbers, strings etc.) which aren’t fetched by a table, view, etc., you need to do the following:

DECLARE
  -- 1. declare a list type
  TYPE STR_LIST_TYPE IS TABLE OF VARCHAR2(15);

  -- 2. declare the variable of the list
  V_STR_VALUES STR_LIST_TYPE;

  -- 3. optional variable to store single values
  V_STR_VALUE VARCHAR2(15);

BEGIN

  -- 4. initialize the list of values to be iterated in a for-loop
  V_STR_VALUES := STR_LIST_TYPE('String 1','String 2');

  -- 5. iterating over the values
  FOR INDX IN V_COLUMN_NAMES.FIRST..V_COLUMN_NAMES.LAST
  LOOP
  
    -- 6. accessing the value itself
    V_STR_VALUE := V_STR_VALUES(INDX);
    
  END LOOP;     
END;
Share Button