Results 1 to 4 of 4
  1. #1
    Join Date
    Jul 2004
    Posts
    40

    Unanswered: Error on executing for loop.

    Hi,

    I want to understand the functionality of EXIT and the following code was written.

    Can anyone spot out the error in the following code?

    create or replace procedure issue10proc
    is
    var varchar2(30);
    var1 integer;
    var2 integer;
    begin
    var1 := 0;
    var := 'anand';
    var2 := 2;
    <<aloop_lable0>>
    FOR var2 IN 1 .. 3
    loop
    if var = 'anand' then exit aloop_lable0;
    var1 := var1 + 1;
    end loop aloop_lable0;
    DBMS_OUTPUT.PUT_LINE(var1);
    end;

    /
    show error
    /

    On compilation following error was thrown,

    Warning: Procedure created with compilation errors.

    Errors for PROCEDURE ISSUE10PROC:

    LINE/COL ERROR
    -------- -----------------------------------------------------------------
    15/5 PLS-00103: Encountered the symbol "LOOP" when expecting one of
    the following:
    if


    Warning: Procedure created with compilation errors.

  2. #2
    Join Date
    May 2004
    Location
    Dominican Republic
    Posts
    721
    Put LOOP word in the same line as the FOR, so it becomes.. FOR var2 IN 1 .. 3 LOOP.

    Also, I have some advices for you.

    (1) One should never declare the counter variable in a FOR LOOP. If you do so, the content of your procedure/function variable will be different for that on the LOOP.
    (2) It is a very bad bad practice to use EXIT in a FOR LOOP. That's why the existence of the INFINITIVE LOOP and the WHILE LOOP.

  3. #3
    Join Date
    Jun 2004
    Location
    Liverpool, NY USA
    Posts
    2,509
    actually the loop can be on the following line with no problems. You problem is that if you have an IF, then you have to have an END IF; The code should be.

    create or replace procedure issue10proc
    is
    var varchar2(30);
    var1 integer;
    var2 integer;
    begin
    var1 := 0;
    var := 'anand';
    var2 := 2;
    <<aloop_lable0>>
    FOR var2 IN 1 .. 3
    loop
    if var = 'anand' then
    exit aloop_lable0;
    end if;
    var1 := var1 + 1;
    end loop aloop_lable0;
    DBMS_OUTPUT.PUT_LINE(var1);
    end;
    Bill
    You do not need a parachute to skydive. You only need a parachute to skydive twice.

  4. #4
    Join Date
    Jan 2004
    Location
    Croatia, Europe
    Posts
    4,094
    Provided Answers: 4
    Position of a "LOOP" didn't cause this error; it is absence of an "END IF;" statement.

    P.S. Sorry ... didn't see your post, Beilstwh
    Last edited by Littlefoot; 07-15-04 at 17:08.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •