Setting pgsql variable from query result

From: "Moravec Jan" <Jan(dot)Moravec(at)ips-ag(dot)cz>
To: "PGSQLN (E-mail)" <pgsql-novice(at)postgresql(dot)org>
Subject: Setting pgsql variable from query result
Date: 2005-11-08 16:11:47
Message-ID: D2BB392CF0C67542891FD639B93325761D1BB2@pex2.corp
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-novice

Hello,

I need to write a DB init script that creates a couple of basic objects in my DB. I need to be able to temporarily store several sequence values so that I can use all of them in my queries later on in the script. Like so (simplified example):

\set fn_id "select nextval('SEQ_LOCALIZED_STR')"
\set sn_id "select nextval('SEQ_LOCALIZED_STR')"
....
other nextvals from the same sequence

insert into PERSON ( ID, FIRST_NAME_LS, SURNAME_LS, ...) values ( nextval('SEQ_PERSON'), :fn_id, :sn_id );

I left out all the quote escaping stuff. Obviously the above does not work because the variables are initialized with the "select..." string literals rather then the sequence values.

Hence my question, is it posible to set a variable from a query result? Or can I use some other mechanism to keep multiple interim sequence values and then use these values in a query. If possible I would like to avoid setting the variable by running external pgsql process like so:

\set fn_id `pgsql -U user -P passw -c "select nextval('SEQ_LOCALIZED_STR')" mydb` - syntax may be incorrect


Thank you,
Jan Moravec

Browse pgsql-novice by date

  From Date Subject
Next Message John DeSoi 2005-11-08 18:56:26 Re: simple or global column names?
Previous Message george young 2005-11-08 15:28:26 simple or global column names?