Click here to Skip to main content
15,843,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am doing a find average code in pl sql but getting error.
Below i am giving tried code with error.
C#
DECLARE
   type namearray IS VARRAY(5) OF VARCHAR2(10);
   type grade IS VARRAY(5) OF INTEGER;
   names namearray;
   marks grade;
   total integer;
   sum integer := 0;
BEGIN
   names := namearray('Kavita', 'Pritam', 'Ayan', 'Rishav', 'Aziz');
   marks := grade(98, 97, 78, 87, 92);
   total := names.count;
   dbms_output.put_line('Total '|| total || ' Students');
   FOR i in 1 .. total LOOP
sum:= sum + marks(i);
dbms_output.put_line(marks(i));
  END LOOP;
dbms_output.put_line(sum / total);
END;


Error
---------------------
C#
ORA-06550: line 14, column 11:
PLS-00103: Encountered the symbol "+" when expecting one of the following:

   (
The symbol "(" was substituted for "+" to continue.
ORA-06550: line 14, column 21:
PLS-00103: Encountered the symbol ";" when expecting one of the following:

   . ( ) * % & - + / at mod remainder rem  ||
The symbol ")" was substituted for ";" to continue.
ORA-06550: line 17, column 26:
PLS-00103: Encountered the symbol "/" when expecting one of the following:

   (


What I have tried:

...................................................................................................
Posted
Updated 31-Oct-16 10:17am

1 solution

The problem is that sum is a reserved word (see Summation - Wikipedia[^]).

Try to change the variable name. For example mysum:
SQL
DECLARE
   type namearray IS VARRAY(5) OF VARCHAR2(10);
   type grade IS VARRAY(5) OF INTEGER;
   names namearray;
   marks grade;
   total integer;
   mysum integer := 0;
BEGIN
   names := namearray('Kavita', 'Pritam', 'Ayan', 'Rishav', 'Aziz');
   marks := grade(98, 97, 78, 87, 92);
   total := names.count;
   dbms_output.put_line('Total '|| total || ' Students');
   FOR i in 1 .. total LOOP
      mysum := mysum + marks(i);
      dbms_output.put_line(marks(i));
  END LOOP;
dbms_output.put_line(mysum / total);
END;
/
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900