Ask the Expert

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;

    Requires Free Membership to View

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

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

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: