1

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.

2
  • Post the fiddle you are using. Commented Apr 30, 2014 at 17:42
  • @Rahul I've included a link to the fiddle at the bottom. Commented Apr 30, 2014 at 17:43

1 Answer 1

3

There are few points to mention as below

  1. No need of cursors ... you can simply do a join between the table employee and department.

With that below is what you should try

CREATE OR REPLACE FUNCTION updateSalary() RETURNS VOID 
AS
$BODY$
  DECLARE sum INT DEFAULT 0;

select sum = sum(emp.salary) 
FROM Employee emp
join Department dept
on emp.Dno = dept.Dno;

    UPDATE department as d SET total_sal = sum 
    FROM employee as e    
    WHERE d.dno = e.dno;
$BODY$
  LANGUAGE plpgsql;

EDIT:

Modified your cursor definition a bit .. give it a try

CREATE OR REPLACE FUNCTION updateSalary() RETURNS VOID AS
$BODY$
  DECLARE 
  sum INTEGER := 0;
  dep CURSOR FOR SELECT Dno FROM Department;
  dep_row Department%ROWTYPE;
  emp CURSOR(dept_Dno INTEGER) FOR <-- change here
    SELECT Dno, Salary FROM Employee WHERE Dno = dept_Dno;
  emp_row Employee%ROWTYPE;
BEGIN
  open dep;
  LOOP
    FETCH dep into dep_row;
      exit when NOT FOUND;
    open emp( dep_row.Dno );
    LOOP
      FETCH emp into emp_row;
        exit when NOT FOUND;
         sum := sum + emp_row.salary;
     END LOOP;
    UPDATE department SET total_sal = sum WHERE department.dno = emp_row.dno;
    close emp;
    sum := 0;
  END LOOP;
  close dep;
END;
$BODY$
  LANGUAGE plpgsql;

You got that error cause data type was not mentioned. what I have used is known as Parameterized Cursors.

Sign up to request clarification or add additional context in comments.

5 Comments

I appreciate your input. I unfortunately do not have a choice but to use cursors as this is an assignment to learn about Stored Procedures and Cursors.
@FatalProphet, ohh!!! you should have mentioned it earlier. need to recheck again.
My apologies, I should've known better. It seems to be a collective agreement that cursors are the devil and should be avoided at all costs. It makes me wonder why even both teaching it in the classroom.
@FatalProphet, it's still tought in classroom; so that you at least know the concept but in practial cursor should be avoided and replaced by simple join for performance reason. Anyways, see edited answer and give it a try.
When I try to build the schema with your updated function, I get the following error Schema Creation Failed: ERROR: missing data type declaration at or near ")":

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.