cancel
Showing results for 
Search instead for 
Did you mean: 

Loop using a variable.

karthik_25
Occasional Contributor

Loop using a variable.

Hi

Is there a way to loop through a variable and with the value in that variable I have to insert into the table.

eg.,

colors = {'BLUE','BLACK','ORANGE','YELLOW'};

loop

for each value (:value)in color
insert into the table
(col1,
col2,
color
)
values
(val1,
val2,
:value
);

end loop;
2 REPLIES
kemo
Trusted Contributor

Re: Loop using a variable.


please check the below example
=======================================
SQL>
SQL> -- display data in the table
SQL> select * from Employee
2 /

no rows selected

SQL>
SQL>
SQL>
SQL>
SQL> BEGIN
2 FOR v_LoopCounter IN 1..10 LOOP
3 INSERT INTO employee (id)
4 VALUES (v_LoopCounter);
5 END LOOP;
6 END;
7 /

PL/SQL procedure successfully completed.

SQL>
SQL> select * from employee;

ID FIRST_NAME LAST_NAME START_DAT END_DATE SALARY CITY DESCRIPTION
---- -------------------- -------------------- --------- --------- ---------- ---------- ---------------
1
2
3
4
5
6
7
8
9
10


10 rows selected.

SQL>
karthik_25
Occasional Contributor

Re: Loop using a variable.

Hi Kemo, Thanks for the reply.

If its a integer counter, i can increment and pass the value to the insert statement.

How to do this if its a set of string variable?

for str_counter in ('STR1', 'STR2','STR3') -- is this type of implementation possible??