John Obey
John Obey

Reputation: 21

Error with while loop in pl sql

I use while loop to calculate the integers from 1 to 10 in Pl Sql.

This is the code:

set serveroutput on;
declare
i number := 0;
sum number := 0;
begin
while i <= 10 loop
sum := sum + i ;
i := i + 1;
end loop;
dbms_output.put_line ( sum );
end;
/

what is the problem here?

This is the error message:

sum := sum + i ;
           *
ERROR at line 6: 
ORA-06550: line 6, column 12: 
PLS-00103: Encountered the symbol "+" when expecting one of the following: 
( 
ORA-06550: line 9, column 28: 
PLS-00103: Encountered the symbol ")" when expecting one of the following: 
(

Upvotes: 2

Views: 335

Answers (1)

Ed Heal
Ed Heal

Reputation: 59997

Try not using the variable name sum.

i.e

set serveroutput on;
declare
i number := 0;
s number := 0;
begin
while i <= 10 loop
s := s + i ;
i := i + 1;
end loop;
dbms_output.put_line ( s );
end;
/

Seems to work

Upvotes: 5

Related Questions