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.

Dig Deeper on Oracle development languages

Data Management
Business Analytics
SearchSAP
TheServerSide.com
Data Center
Content Management
HRSoftware
Close