Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 ")": 
share|improve this question
    
Please link to the SQLFiddle in question. –  Craig Ringer yesterday
    
@CraigRinger I've included the link –  FatalProphet yesterday
add comment

1 Answer

up vote 2 down vote accepted

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;
share|improve this answer
    
I've included your changes and am now getting this error Schema Creation Failed: ERROR: syntax error at or near ":=": –  FatalProphet yesterday
    
Probably you did not remove set in the second assignment: 'sum := 0;' –  klin yesterday
1  
@FatalProphet, I have already edited the other post (related) ... give it a try now. should work fine. –  Rahul yesterday
1  
'select updateSalary();' in plain SQL; or 'perform updateSalary();' inside a function. –  klin yesterday
1  
@FatalProphet, just call it like updateSalary() –  Rahul yesterday
show 2 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.