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 ")":
klin
123k15 gold badges241 silver badges263 bronze badges
asked May 1, 2014 at 0:35
Talen Kylon
1,9887 gold badges36 silver badges64 bronze badges
-
Please link to the SQLFiddle in question.Craig Ringer– Craig Ringer2014年05月01日 00:51:56 +00:00Commented May 1, 2014 at 0:51
-
@CraigRinger I've included the linkTalen Kylon– Talen Kylon2014年05月01日 00:54:13 +00:00Commented May 1, 2014 at 0:54
1 Answer 1
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;
answered May 1, 2014 at 0:55
klin
123k15 gold badges241 silver badges263 bronze badges
Sign up to request clarification or add additional context in comments.
6 Comments
Talen Kylon
I've included your changes and am now getting this error Schema Creation Failed: ERROR: syntax error at or near ":=":
klin
Probably you did not remove set in the second assignment: 'sum := 0;'
Rahul
@FatalProphet, I have already edited the other post (related) ... give it a try now. should work fine.
Talen Kylon
Thanks folks, one more question. How do I call this procedure now ?
klin
'select updateSalary();' in plain SQL; or 'perform updateSalary();' inside a function.
|
lang-sql