Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

postgresql - Cannot create plpgsql function using psql -f filename option

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...