pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Ron <ronljohnsonjr@gmail.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: pgsql-general@lists.postgresql.org
Subject: Re: psql \set variables in crosstab queries?
Date: Sat, 4 Mar 2023 23:03:37 -0600
Message-ID: <7fa041bf-bca9-4a2b-f1cc-ebdcb7b76dd2@gmail.com> (raw)
In-Reply-To: <3054299.1677979338@sss.pgh.pa.us>
References: <1337b328-44f7-7f5b-cc1d-b8d11aced2cd@gmail.com>
	<3054299.1677979338@sss.pgh.pa.us>

On 3/4/23 19:22, Tom Lane wrote:
> Ron <ronljohnsonjr@gmail.com> writes:
>> According to
>> https://www.postgresql.org/docs/12/app-psql.html#APP-PSQL-VARIABLES and
>> experience, variables don't interpolate inside of string literals:
>> "
>> Variable interpolation will not be performed within quoted SQL literals and
>> identifiers. Therefore, a construction such as ':foo' doesn't work to
>> produce a quoted literal from a variable's value.
>> "
>> But crosstab takes text strings as parameters.  How then do you use \set
>> variables in crosstab queries?
> If you read a little further, you'll find out the syntax for converting
> the value of a psql variable to a SQL string literal:
>
> regression=# \set foo BAR
> regression=# select ':foo';
>   ?column?
> ----------
>   :foo
> (1 row)
>
> regression=# select :'foo';
>   ?column?
> ----------
>   BAR
> (1 row)
>
> What the server got in the last case was "select 'BAR';".

postgres=# \set foo BAR
postgres=# select :'foo';
  ?column?
----------
  BAR
(1 row)

postgres=# select $$ :foo $$;
  ?column?
----------
   :foo
(1 row)

postgres=# select $$ :'foo' $$;
  ?column?
----------
   :'foo'
(1 row)





-- 
Born in Arizona, moved to Babylonia.





view thread (6+ messages)  latest in thread

Message-ID: <7fa041bf-bca9-4a2b-f1cc-ebdcb7b76dd2@gmail.com>
Permalink:  ../7fa041bf-bca9-4a2b-f1cc-ebdcb7b76dd2@gmail.com/
Also on:    postgresql.org/message-id/7fa041bf-bca9-4a2b-f1cc-ebdcb7b76dd2@gmail.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: ronljohnsonjr@gmail.com, tgl@sss.pgh.pa.us
  Subject: Re: psql \set variables in crosstab queries?
  In-Reply-To: <7fa041bf-bca9-4a2b-f1cc-ebdcb7b76dd2@gmail.com>
  References: <1337b328-44f7-7f5b-cc1d-b8d11aced2cd@gmail.com> <3054299.1677979338@sss.pgh.pa.us> <7fa041bf-bca9-4a2b-f1cc-ebdcb7b76dd2@gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox