Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

For each string, execute a function/procedure

I'd like to loop through a list of strings and execute a function/procedure with each string as the argument.

What's the best alternative to the following generic code (since it's not legal):

set serveroutput on;  
begin
    FOR r IN ('The', 'Quick', 'brown', 'fox')
    LOOP
        dbms_output.put_line( r );
   END LOOP;
end;

I assume there might be pattern for this.

like image 857
OwlCity Avatar asked Feb 11 '10 03:02

OwlCity


People also ask

Can we use procedure in function in Oracle?

You can view existing functions and procedures in Object Browser. The SQL CREATE PROCEDURE statement is used to create stored procedures that are stored in the database. The SQL CREATE FUNCTION statement is used to create stored functions that are stored in an Oracle database.


1 Answers

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_STR_VALUES.FIRST..V_STR_VALUES.LAST
  LOOP

    -- 6. accessing the value itself
    V_STR_VALUE := V_STR_VALUES(INDX);

  END LOOP;     
END;
like image 173
Duc Tran Avatar answered Oct 20 '22 00:10

Duc Tran