【问题标题】:PL/PgSQL confusing errorPL/PgSQL 混淆错误
【发布时间】:2011-05-12 10:49:58
【问题描述】:

我为 Postgres 编写了以下函数,当通过 pgAdmin 安装时,它在我的本地 Windows 机器上运行良好。当我尝试将它添加到基于 linux 的服务器安装时,它会引发编译错误:

查询:选择 $1 (split_part($2,'', $3)) 上下文:第 34 行附近的 PL/PgSQL 函数“splitwords”中的 SQL 语句

CREATE OR REPLACE FUNCTION splitwords(text, int)
  RETURNS text AS
$BODY$
DECLARE
inwords ALIAS FOR $1;
posn INTEGER;
existcount INTEGER;
incurrdataid ALIAS FOR $2;
currdataid INTEGER;
currwordid INTEGER;
length INTEGER;
wordpos INTEGER;
newword TEXT;
BEGIN
currdataid:=incurrdataid;
currdataid:=currdataid-1; --corrects for auto-increment error
posn:=1;
WHILE posn<11 LOOP
IF split_part(inwords,' ',posn)='' THEN
-- If no more words are available
    EXIT;
ELSE
--If not at the end of the words
    IF (SELECT wordID FROM words WHERE word=split_part(inwords,' ',posn))>0 THEN
    --If word is already in lexicon
        currwordid:=(SELECT wordID FROM words WHERE word=split_part(inwords,' ',posn))::INTEGER;
        existcount:= (SELECT count FROM words WHERE word=split_part(inwords,' ',posn))::INTEGER;
        UPDATE words SET count=existcount+1 WHERE word=split_part(inwords,' ',posn);
        INSERT INTO wordsdata(wordid,dataid) VALUES (currwordid,currdataid);
        posn:=posn+1;
    ELSE
    --If word is new
        newword=split_part(inwords,' ',posn);
        INSERT INTO words(word,count) VALUES (newword,1);
        currwordid:=(SELECT wordID FROM words WHERE word=split_part(inwords,' ',posn))::INTEGER;
        INSERT INTO wordsdata(wordid,dataid) VALUES (currwordid,currdataid);
        length:=length(split_part(inwords,' ',posn));
        wordpos:=1;
        WHILE wordpos<(length+1) LOOP
            INSERT INTO searchchar(searchstr,wordid) VALUES (substring(split_part(inwords,' ',posn),1,wordpos),currwordid);
            wordpos=wordpos+1;
        END LOOP;
        posn:=posn+1;
    END IF;
END IF;
END LOOP;

RETURN 'rows added';
END
$BODY$
  LANGUAGE plpgsql VOLATILE
  COST 100;

搜索了谷歌和文档,但我找不到任何相关内容。我很困扰!非常感谢您提供的任何帮助!

【问题讨论】:

    标签: postgresql plpgsql


    【解决方案1】:

    尚未阅读您的函数,但错误消息通常表示变量与列同名。使用_ 前缀变量名称通常是避免该消息的好方法,它使 plpgsql 函数更具可读性。

    此外,您的代码中至少出现了一次=,看起来应该是:=。 plpgsql 对这些东西很宽容,但你不应该指望它。

    【讨论】:

    • 第 34 行有保留字 count。这将是我的第一个猜测。
    猜你喜欢
    • 1970-01-01
    • 2012-03-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-07
    • 1970-01-01
    相关资源
    最近更新 更多