How to write a for loop in Oracle sqlplus?

I am trying to write a for loop in the Oracle sqlplus interface. When writing a loop statement, pressing enter, I get an error:

SQL> for i in 1..10 loop SP2-0734: unknown command beginning "for i in 1..." - rest of line ignored. SQL> 

Is there something wrong with my for clause?

+7
source share
2 answers

The loop uses PL / SQL. Try wrapping PL / SQL in a BEGIN / END block.

If you need to declare variables, start with DECLARE. Something like that:

 set serveroutput on begin for a in 1..10 loop dbms_output.put_line('a='||to_char(a)); end loop; end; / 

Hope this helps.

PS Note that set serveroutput on is an SQL * Plus command, not a part of PL / SQL. It just includes the output, so you will see the result of the dbms_output.put_line () function.

+17
source

sqlplus is not a language, but an interface to Oracle, into which you can enter SQL or PL / SQL.

In this case, use a simple anonymous pl / sql block

 begin for i in 1..10 loop -- some great stuff goes here end loop; end / 
+6
source

All Articles