【问题标题】:Cannot create plpgsql function using psql -f filename option无法使用 psql -f 文件名选项创建 plpgsql 函数
【发布时间】:2015-07-05 11:02:11
【问题描述】:

我正在使用 Postgres 8.4,并尝试使用以下命令从命令行运行 create function ... 脚本

psql dbname -U username -f filename

psql -f filename -d dbname -U username 

它总是会导致以下错误

psql:mergenodedata.sql:40:错误:“创建”处或附近的语法错误 第 1 行:创建或替换 FUNCTION updNode (oldnodename varchar, ne...

第 40 行是文件的结尾

$$ LANGUAGE plpgsql;

如果我将文件的文件内容剪切并粘贴到 pgadmin 或打开的 psql 会话中,那么 create function 就完美了。

代码是

create or replace FUNCTION updNode (oldnodename varchar, newnodename varchar, scnname varchar, cid integer) returns void AS $$
declare
        oldnodeid integer;
        newnodeid integer;
        scnid integer;
        newcount integer;
        oldcount integer;

BEGIN

raise notice 'doing %', oldnodename;

select id from nodes where nodename = oldnodename and cityid = cid into oldnodeid;
select id from nodes where nodename = newnodename and cityid = cid into newnodeid;
select id from scenario where name = scnname and cityid = cid into scnid;

raise notice 'oldnodeid is %', oldnodeid;
raise notice 'newnodeid is %', newnodeid;
raise notice 'scnid is %', scnid;

select count(*) from collection_node_result where node1id = newnodeid and scenario_collection_id in (
        select id from scenario_collection where scenarioid = scnid into newcount
);

raise notice 'newcount is %', newcount;

select count(*) from collection_node_result where node1id = oldnodeid and scenario_collection_id in (
        select id from scenario_collection where scenarioid = scnid into oldcount
);

raise notice 'oldcount is %', oldcount;


update collection_node_result set node1id = newnodeid where node1id = oldnodeid and scenario_collection_id in (
        select id from scenario_collection where scenarioid = scnid
);


END;
$$ LANGUAGE plpgsql;

我引用的其他页面是

Run plpgsql program to update the data in table

http://www.postgresql.org/docs/8.4/static/plpgsql-development-tips.html

【问题讨论】:

  • 不相关的有趣事实:老实说,我现在唯一的愿望是让“传奇徽章”天数计数器切换......我忘记了:一旦你从 任何事情在某一天。而那个被删除的问题......仍然显示为 +40 ... -40 。所以这 4 次投票仍然有助于第 149 天。

标签: postgresql plpgsql


【解决方案1】:

它看起来像损坏的文件 - BOM http://en.wikipedia.org/wiki/Byte_order_mark 或结束符号“^z”可以强制执行一些问题。在一些 hexeditor 中查看文件并检查文件的开始和结束。当我使用在其他操作系统上创建的文件时,我遇到了类似的问题。

【讨论】:

  • 谢谢vi myfile.sql 然后:set fileencoding=ascii
猜你喜欢
  • 2012-11-15
  • 2016-02-26
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多