Search code examples
oracle-databaseloopsplsql

Why label cannot be declared before end


Below code works perfectly

DECLARE
 x NUMBER := 0;
BEGIN
 LOOP
  IF x > 5 THEN
   GOTO my_label;
  END IF;
  dbms_output.put_line(x);
  x := 1 + x;
 END LOOP;

 << my_label >> 
 x := 100;
END;

but if i remove x := 100 then its showing below error.

ORA-06550: line 13, column 1:
PLS-00103: Encountered the symbol "END" when expecting one of the following:
 ( begin case declare exit for goto if loop mod null raise
   return select update while with <an identifier>
   <a double-quoted delimited-identifier> <a bind variable> <<
   continue close current delete fetch lock insert open rollback
   savepoint set sql execute commit forall merge standard pipe
   purge json_object

I know labels should appear directly in front of the thing they’re labeling, which must be an executable statement even if it is merely the NULL statement. So is END; not considering as a statement then ?

I need to know why its throwing error. I also refer Oracle docs but it doesn't mention anything like this.


Solution

  • Documentation changes between versions; sometimes newer versions don't contain useful information which existed in previous versions.

    In 10g GOTO statement, it says:

    The GOTO statement branches unconditionally to a statement label or block label. The label must be unique within its scope and must precede an executable statement or a PL/SQL block. The GOTO statement transfers control to the labelled statement or block

    END; is not an executable statement; NULL; (or your x := 100;) are.