I'm designing a function that utilizes a couple cursors and I am new to Postgres so I am unaware of any small syntax issues I'm making. I'm using SQLFIDDLE to implement this and the error I am receiving is:
Schema Creation Failed: ERROR: syntax error at or near "+":
And the function looks as follows,
CREATE OR REPLACE FUNCTION updateSalary() RETURNS VOID AS
$BODY$
DECLARE
DECLARE sum INT DEFAULT 0;
emp CURSOR FOR SELECT Dno, Salary FROM Employee;
emp_row RECORD;
dep CURSOR FOR SELECT Dno FROM Department;
dep_row RECORD;
BEGIN
open dep;
LOOP
FETCH dep into dep_row;
exit when dep_row = null;
open emp;
LOOP
FETCH emp into emp_row;
exit when emp_row = null;
if emp_row.Dno =dep_row.Dno then
SET sum = sum + emp_row.salary;
end if;
END LOOP;
UPDATE department SET total_sal = sum WHERE department.dno = emp_row.dno;
close emp;
SET sum = 0;
END LOOP;
close dep;
END;
$BODY$
LANGUAGE plpgsql;
I am sure there are a ton of other errors in here so if any jump out at you please let me know and save me from the wonderful joys of debugging postgresql.
Here is the Fiddle http://sqlfiddle.com/#!15/a1a45/3
For some reason, the function isn't saved into that Fiddle. I think in order for a Fiddle to save it must be able to be built.