0

I am having trouble debugging some syntax issues with my postgresql function. The error doesn't indicate a line that the issue is on and I am too new to Postgres to recognize what's wrong. I am using SQLFiddle for testing. SQLFiddle link is http://sqlfiddle.com/#!15/266ef

Function I am using

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) CURSOR FOR 
    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;
         SET sum := sum + emp_row.salary;
     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;

Error I am receiving

Schema Creation Failed: ERROR: missing data type declaration at or near ")": 
2
  • Please link to the SQLFiddle in question. Commented May 1, 2014 at 0:51
  • @CraigRinger I've included the link Commented May 1, 2014 at 0:54

1 Answer 1

2

Cursor argument must have a type declared (and remove second word 'cursor'):

...
emp CURSOR(dept_Dno integer) FOR 
    SELECT Dno, Salary FROM Employee WHERE Dno = dept_Dno;
...

Assignments without keyword 'set':

 sum := sum + emp_row.salary;
Sign up to request clarification or add additional context in comments.

6 Comments

I've included your changes and am now getting this error Schema Creation Failed: ERROR: syntax error at or near ":=":
Probably you did not remove set in the second assignment: 'sum := 0;'
@FatalProphet, I have already edited the other post (related) ... give it a try now. should work fine.
Thanks folks, one more question. How do I call this procedure now ?
'select updateSalary();' in plain SQL; or 'perform updateSalary();' inside a function.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.