diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2013-02-02 17:06:38 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2013-02-02 17:06:38 -0500 |
commit | d2d153fdb08053d655bd0fef14187eed6a674193 (patch) | |
tree | d5b988ce6d712fcec154511e90af87b73d34d3e2 /doc/src | |
parent | 101d6ae755656b675b7c18db655249511982b780 (diff) | |
download | postgresql-d2d153fdb08053d655bd0fef14187eed6a674193.tar.gz postgresql-d2d153fdb08053d655bd0fef14187eed6a674193.zip |
Create a psql command \gset to store query results into psql variables.
This eases manipulation of query results in psql scripts.
Pavel Stehule, reviewed by Piyush Newe, Shigeru Hanada, and Tom Lane
Diffstat (limited to 'doc/src')
-rw-r--r-- | doc/src/sgml/ref/psql-ref.sgml | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index eb5e3b19048..233f747163e 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1621,6 +1621,46 @@ Tue Oct 26 21:40:57 CEST 1999 </varlistentry> <varlistentry> + <term><literal>\gset</literal> [ <replaceable class="parameter">prefix</replaceable> ]</term> + + <listitem> + <para> + Sends the current query input buffer to the server and stores the + query's output into <application>psql</> variables (see <xref + linkend="APP-PSQL-variables" endterm="APP-PSQL-variables-title">). + The query to be executed must return exactly one row. Each column of + the row is stored into a separate variable, named the same as the + column. For example: +<programlisting> +=> <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput> +-> <userinput>\gset</userinput> +=> <userinput>\echo :var1 :var2</userinput> +hello 10 +</programlisting> + </para> + <para> + If you specify a <replaceable class="parameter">prefix</replaceable>, + that string is prepended to the query's column names to create the + variable names to use: +<programlisting> +=> <userinput>SELECT 'hello' AS var1, 10 AS var2</userinput> +-> <userinput>\gset result_</userinput> +=> <userinput>\echo :result_var1 :result_var2</userinput> +hello 10 +</programlisting> + </para> + <para> + If a column result is NULL, the corresponding variable is unset + rather than being set. + </para> + <para> + If the query fails or does not return one row, + no variables are changed. + </para> + </listitem> + </varlistentry> + + <varlistentry> <term><literal>\h</literal> or <literal>\help</literal> <literal>[ <replaceable class="parameter">command</replaceable> ]</literal></term> <listitem> <para> |