0

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

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
4

1 回答 1

2

将您的更新声明更改为

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

或者,基本上,在等号之前去掉“:”。PL/SQL 和 plpgsql:=用于赋值和比较,但 SQL 使用=.

分享和享受。

于 2013-05-16T10:50:07.220 回答