2013-05-16 129 views
0

我是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 
+0

我的专栏名称是'weight',如果用'weight'替换'weights',则没有动作执行。 –

+0

包含正在使用的PostgreSQL版本至关重要。另外,您应该学会不要从您引用的帖子中引用语言名称plpgsql。 –

回答

2

更改您的UPDATE语句

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

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

分享和享受。

+0

谢谢@bob贾维斯 –

+0

错误是删除,但数据不会更新:( –

+0

你有没有提交任何地方的变化吗?我看不到在程序中的COMMIT –

相关问题