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 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…