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.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With