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

  2 Responses to “Iterating over a list of values in Oracle PL/SQL”

  1. Hi,
    nice tip!
    In line 17 you should replace V_COLUMN_NAMES by V_STR_VALUES.
    However, it solved my problem 😉

    Frank

  2. should be FOR INDX IN V_STR_VALUES.FIRST..V_STR_VALUES.LAST

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

(required)

(required)