Skip site navigation (1) Skip section navigation (2)

proposal - assign result of query to psql variable

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)postgresql(dot)org>
Subject: proposal - assign result of query to psql variable
Date: 2012-07-26 05:13:01
Message-ID: CAFj8pRC5djqVUaJvrstCi66f=qccFE-cXG=RwejvgMFGOUb7Lg@mail.gmail.com (view raw or flat)
Thread:
Lists: 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

Responses

pgsql-hackers by date

Next:From: Tom LaneDate: 2012-07-26 05:36:17
Subject: Re: proposal - assign result of query to psql variable
Previous:From: Alvaro HerreraDate: 2012-07-26 03:58:29
Subject: Re: filenames in pg_basebackup

Privacy Policy | About PostgreSQL
Copyright © 1996-2014 The PostgreSQL Global Development Group