SQL select return into PSQL variables.

From: "Little, Douglas" <DOUGLAS(dot)LITTLE(at)orbitz(dot)com>
To: "pgsql-general(at)postgresql(dot)org" <pgsql-general(at)postgresql(dot)org>
Subject: SQL select return into PSQL variables.
Date: 2010-02-18 15:33:41
Message-ID: A434C531E37AD442815608A769550D80592F9A5855@EGEXCMB01.oww.root.lcl
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hello,
I want to get a sql select output into a psql variable. Any ideas how I might need to do this.

My script executes a function which returns a TESTID. I'd like to imbed the testid in the script output filenames.

I see that psql can set environment variables with the psql \i command.
But got any idea how I can get sql output into a psql variable?

Thanks
Doug

psql
orbitz=# \!testvar=1234
orbitz=# \!export testvar
orbitz=# \!echo $testvar
1234
orbitz=# \q
-bash-3.00$ echo $testvar
1234

But I'm unsure how I can get the pgsql return code into a psql variable
The shell script
current
psql p1gp1 <<QUIT >>$LOGFile 2>&1
\set ON_ERROR_STOP
select da_test.QATestBuild(false)
QUIT

I'd like to do something like this from my shell script
psql p1gp1 <<QUIT >>$LOGFile 2>&1
\set ON_ERROR_STOP
select da_test.QATestBuild(false) into :testid
\!testid=:testid
\!export $testid
QUIT
Echo $testid
1234

Doug Little
Sr. Data Warehouse Architect | Enterprise Data Management | Orbitz Worldwide
500 W. Madison, Suite 1000 Chicago IL 60661| Office 312.260.2588 | Fax 312.894.5164 | Cell 847-997-5741
Douglas(dot)Little(at)orbitz(dot)com<mailto:Douglas(dot)Little(at)orbitz(dot)com>

[cid:image002(dot)jpg(at)01CAB07C(dot)769E4E80] orbitz.com<http://www.orbitz.com/> | ebookers.com<http://www.ebookers.com/> | hotelclub.com<http://www.hotelclub.com/> | cheaptickets.com<http://www.cheaptickets.com/> | ratestogo.com<http://www.ratestogo.com/> | asiahotels.com<http://www.asiahotels.com/>

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2010-02-18 15:42:05 Re: errmsg and multi-byte strings.
Previous Message Adrian Klaver 2010-02-18 14:56:45 Re: Doubts about oid