PLS-00103:在期望以下之一时遇到符号“+”:(

PLS-00103: Encountered the symbol "+" when expecting one of the following: (

declare 
    i number;
    sum number;
begin
    i:=1;
    sum:=0;
    
    for i in 1..100 loop
        if MOD(i,2) != 0 then
            sum:= sum + i;
             dbms_output.put_line(i);
        end if;
    end loop;
    dbms_output.Put_line(sum);
end;

sum是保留字,保留给built-in函数使用。将变量重命名为 v_sum(例如)。

SQL> set serveroutput on
SQL>
SQL> DECLARE
  2     v_sum  NUMBER;
  3  BEGIN
  4     v_sum := 0;
  5     FOR i IN 1 .. 100 LOOP
  6        IF MOD (i, 2) != 0 THEN
  7           v_sum := v_sum + i;
  8           -- DBMS_OUTPUT.put_line (i);
  9        END IF;
 10     END LOOP;
 11     DBMS_OUTPUT.Put_line (v_sum);
 12  END;
 13  /
2500

PL/SQL procedure successfully completed.

SQL>