Postgres函数的语法问题

时间:2014-05-01 00:35:47

标签: postgresql plpgsql

我在使用postgresql函数调试一些语法问题时遇到问题。该错误并不表示该问题所在的一条线,而且我对Postgres太过新,无法识别出错误。我正在使用SQLFiddle进行测试。 SQLFiddle链接是http://sqlfiddle.com/#!15/266ef

我正在使用的功能

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;

我收到错误

Schema Creation Failed: ERROR: missing data type declaration at or near ")": 

1 个答案:

答案 0 :(得分:2)

Cursor参数必须声明一个类型(并删除第二个单词' cursor'):

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

没有关键字的分配'设置':

 sum := sum + emp_row.salary;