尝试运行plpgsql函数时出现语法错误42601

时间:2013-05-16 09:55:31

标签: sql postgresql plpgsql

我是PL/pgSQL的新手。尝试将数据插入表时,以下函数会生成错误。这个link定义了错误,但我无法理解这个链接问题。

CREATE OR REPLACE FUNCTION updateScore() 
RETURNS void AS
$$
 DECLARE
 singleTopicCriteriaPercentage DECIMAL(6,6);
 sitePercentage                DECIMAL(6,6);
 singleSiteCriteriaPercentage  DECIMAL(6,6);
 totalSocre                    DECIMAL(6,6);

 cursor1 CURSOR FOR select id from sitereviews order by id;
 cursor2 CURSOR FOR select weight into rating from sitereviews_ratingcriteria where site_id = id;

 id              sitereviews.id%TYPE;
 weights         sitereviews_ratingcriteria.weight%TYPE;

 BEGIN
 singleTopicCriteriaPercentage := (10.0 / 120.0) * 100.0;
 sitePercentage := 0.0;
 singleSiteCriteriaPercentage := 0.0;
 totalSocre := 0.0;

 OPEN cursor1;
 LOOP
 FETCH cursor1 INTO id;
 EXIT WHEN NOT FOUND;
 totalSocre := 0.0;

OPEN cursor2;
LOOP
FETCH cursor2 INTO weights;
EXIT WHEN NOT FOUND;
    sitePercentage := singleTopicCriteriaPercentage * weights;
    singleSiteCriteriaPercentage :=  (sitePercentage / 100) * 10;
    totalSocre := singleSiteCriteriaPercentage + totalSocre;
END LOOP;
CLOSE cursor2;

update sitereviews set weights := round(totalSocre)  WHERE CURRENT OF cursor1;
END LOOP
CLOSE cursor1;
END;
$$ LANGUAGE 'PLPGSQL'

以下是编译时错误:

ERROR:  syntax error at or near "$1"
LINE 1: update sitereviews set  $1  := round( $2 ) WHERE CURRENT OF ...
                            ^
QUERY:  update sitereviews set  $1  := round( $2 ) WHERE CURRENT OF  $3 
CONTEXT:  SQL statement in PL/PgSQL function "updatescore" near line 35

********** Error **********

ERROR: syntax error at or near "$1"
SQL state: 42601
Context: SQL statement in PL/PgSQL function "updatescore" near line 35

1 个答案:

答案 0 :(得分:2)

将更新语句更改为

update sitereviews set set weights = round(totalSocre)  WHERE CURRENT OF cursor1

或者,基本上,在等号前放下“:”。 PL / SQL和plpgsql使用:=进行赋值和比较,但SQL使用=

分享并享受。