proposal - assign result of query to psql variable - Mailing list pgsql-hackers

From Pavel Stehule
Subject proposal - assign result of query to psql variable
Date
Msg-id CAFj8pRC5djqVUaJvrstCi66f=qccFE-cXG=RwejvgMFGOUb7Lg@mail.gmail.com
Whole thread Raw
Responses Re: proposal - assign result of query to psql variable  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Hello

we cannot actually store result of query to psql variable

I propose a new slash statement "\eset for this purpose

Syntax:

\eset variable [, variable [..]] query --  it raise exception when
more than one row is returned or when no row is returned

Usage:

\eset var1, var2 select version(), current_date


Current workaround is not friendly and it is not usable for more than
one target variable
 http://stackoverflow.com/questions/11654244/how-to-bind-a-sql-query-return-value-to-a-psql-variable/11654676#11654676

postgres=# \set myvar `psql -A -t -c "select version()" postgres `
postgres=# \echo :myvar
PostgreSQL 9.1.4 on x86_64-unknown-linux-gnu, compiled by gcc (GCC)
4.7.0 20120507 (Red Hat 4.7.0-5), 64-bit

Regards

Pavel


pgsql-hackers by date:

Previous
From: Alvaro Herrera
Date:
Subject: Re: filenames in pg_basebackup
Next
From: Tom Lane
Date:
Subject: Re: proposal - assign result of query to psql variable