8

I'm trying to write a for loop in Oracle sqlplus interface. When writing the loop statement an 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 loop clause?

2 Answers 2

18

For loop is a PL/SQL construct. Try wrapping your PL/SQL in BEGIN/END block.

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

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

Hope that helps.

PS Note that set serveroutput on is a SQL*Plus command, and not part of PL/SQL. It just turns on output so you'll see the output from the dbms_output.put_line() function.

Sign up to request clarification or add additional context in comments.

4 Comments

this one is for oracle sqlplus interface, not for PL/SQL block
What are you talking about? He's working from SQL*Plus, trying to execute PL/SQL, which is exactly what my question answered, did it not?
There's no need for the declare section a is implicitly defined.
You're absolutely right, Ben. I corrected the code above. Thanks.
6

sqlplus isn't a language but an interface to Oracle in which you can enter SQL or PL/SQL.

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

begin
  for i in 1..10
  loop
    -- some great stuff goes here

  end loop;
end
/

1 Comment

Sqlplus is a language, a kind of scripting language with access to the database and the OS shell, but it's not a fully fledged programming language.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.