Q

Encountered the symbol "ELSE" when expecting one of the following

I am a beginner to Oracle. I am getting this error: "ORA-06550: line 21, column 1: PLS-00103: Encountered the symbol "ELSE" when expecting one of the following:"

I am a beginner to Oracle. I am getting this error:

ORA-06550: line 21, column 1:
PLS-00103: Encountered the symbol "ELSE" when expecting one of the following:

begin case declare end exit for goto if loop mod null pragma raise return select update while with <an identifier> <a double-quoted delimited-identifier> <a bind variable> << close current delete fetch lock insert open rollback savepoint set sql execute commit forall merge <a single-quoted SQL string> pipe

ORA-06550: line 23, column 1:
PLS-00103: Encountered the symbol "END"

The source code is:

declare
A NUMBER;
B NUMBER;
C NUMBER;
BEGIN
A:=&A;
B:=0;
C:=1;
IF A<10 THEN
 IF A<=5 THEN
   FOR I IN 1..A
  LOOP
    B:=B+I;
  END LOOP ;
INSERT INTO TEMP(VALUE,RESULT) VALUES(A,B);  ELSE FOR I IN 1..A
  LOOP
    C:=C*I;
  END LOOP ;
INSERT INTO TEMP(VALUE,RESULT) VALUES(A,C); 
ELSE DBMS_OUTPUT.PUT_FILE('ENTER A VALID NUMBER'); END IF; END;
When working with IF-THEN-ELSE, you need to ensure ELSE comes right after the IF-THEN clause. In your case, you have an INSERT statement before the ELSE clause, so an error is raised. You might have to use BEGIN and END statements to set things aside, similar to the following:
IF A<=5 THEN
BEGIN
   FOR I IN 1..A
   LOOP
     B:=B+I;
   END LOOP ;
   INSERT INTO TEMP(VALUE,RESULT) VALUES(A,B);  
ELSE 
BEGIN
   FOR I IN 1..A
   LOOP
     C:=C*I;
   END LOOP ;
INSERT INTO TEMP(VALUE,RESULT) VALUES(A,C); 
END IF;
In the above code, if A<=5, then the FOR loop is executed and the INSERT statement is executed. Else, the next FOR loop and INSERT statement executed. I denote the end of the ELSE clause with the END IF command.

I'm not sure if this is your intended application logic, but it does illustrate the point.

This was first published in January 2007

Dig deeper on Using Oracle PL-SQL

Pro+

Features

Enjoy the benefits of Pro+ membership, learn more and join.

Have a question for an expert?

Please add a title for your question

Get answers from a TechTarget expert on whatever's puzzling you.

You will be able to add details on the next page.

0 comments

Oldest 

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to:

SearchDataManagement

SearchBusinessAnalytics

SearchSAP

SearchSQLServer

TheServerSide

SearchDataCenter

SearchContentManagement

SearchFinancialApplications

Close