我是PL/pgSQL
的新手。在嘗試向表中插入數據時,以下函數會產生錯誤。這link定義了錯誤,但我無法理解此鏈接問題。嘗試運行plpgsql函數時出現語法錯誤42601
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
我的專欄名稱是'weight',如果用'weight'替換'weights',則沒有動作執行。 –
包含正在使用的PostgreSQL版本至關重要。另外,您應該學會不要從您引用的帖子中引用語言名稱plpgsql。 –