无法使用psql -f filename选项创建plpgsql函数

时间:2015-04-27 05:54:44

标签: postgresql plpgsql

我使用的是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

1 个答案:

答案 0 :(得分:2)

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