Cannot create plpgsql function using psql -f filename option

可紊 提交于 2019-12-13 09:46:58

问题


I am using Postgres 8.4 and I have tried to run a create function ... script from the command line using

psql dbname -U username -f filename

or

psql -f filename -d dbname -U username 

and it always results in the following error

psql:mergenodedata.sql:40: ERROR: syntax error at or near "create" LINE 1: create or replace FUNCTION updNode (oldnodename varchar, ne...

where line 40 is the end of the file

$$ LANGUAGE plpgsql;

If I cut -and-paste file contents of the file into pgadmin or an open psql session, then the create function would perfectly.

The code is

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;

Other pages I have refernced are

Run plpgsql program to update the data in table

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


回答1:


It looks like broken file - some problems can be enforced by BOM http://en.wikipedia.org/wiki/Byte_order_mark or ending symbol "^z". Look on file in some hexeditor and check start and check end of file. I had a similar problems, when I used a files created on other operation system.



来源:https://stackoverflow.com/questions/29888181/cannot-create-plpgsql-function-using-psql-f-filename-option

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!