PL/SQL PLS-00103 on adding two variables
i'm new to pl/sql and trying to print even numbers up till 100 using the following code:
Declare
i number;
开发者_如何转开发 sum number:=0;
x number:=2;
n number;
Begin
for i in 1..100 loop
if (i%x=0) then
n:=i;
sum:=sum+n;
end if;
end loop;
dbms_output.put_line(sum);
end;
i get this error
ERROR at line 10:
ORA-06550: line 10, column 12:
PLS-00103: Encountered the symbol "+" when expecting one of the following:
(
ORA-06550: line 13, column 26:
PLS-00103: Encountered the symbol ")" when expecting one of the following:
(
Help? :(
There is no % operator in PL/SQL; use the mod(i,x) function instead. Anyway, you don't need the n variable. sum:=sum+i will do. sum might be a reserved word, use s instead. \
Now:
Declare
i number;
sum number:=0;
Begin
for i in 1..100 loop
if (mod(i,2)=0) then
sum:=sum+i;
end if;
end loop;
dbms_output.put_line(sum);
end;
sameproblem :(
There is no % operator in PL/SQL; use the mod(i,x)
function instead. Anyway, you don't need the n variable. sum:=sum+i
will do. But: sum
is a PL/SQL keyword, use s instead.
Declare
i number;
sum1 number:=0;
x number:=2;
n number;
Begin
for i in 1..100 loop
if(i mod x =0)then
n:=i;
sum1:=sum1+n;
end if;
end loop;
dbms_output.put_line(sum1);
end;
/
Reason :
The "sum" is one of the aggregate function ,so we can't use this as variable name.
精彩评论