Oracle gem of the day: foreach in PL/SQL

jdavidb on 2006-05-03T14:28:45

Of course, it ain't pretty, folks:

In Perl:

my @items = qw(item1 item2);
foreach my $item (@items)
{
  take_action($item);
}

In PL/SQL:

DECLARE
  TYPE items_t IS TABLE OF VARCHAR2(80);  -- note the strong typing constraint
  items items_t := items_t('item1', 'item2');
  items_index NUMBER := 1;
BEGIN
  <>
  WHILE items_index IS NOT NULL
  LOOP
    DECLARE
      item VARCHAR2(80) := items(items_index);
    BEGIN
      take_action(item);
      items_index := items.NEXT(items_index);
    END;
  END LOOP items_loop;
END;
/


Repeat

jdavidb on 2006-07-19T21:01:46

Note: this version has better syntax.