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

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';".

			regards, tom lane





view thread (6+ messages)  latest in thread

Message-ID: <3054299.1677979338@sss.pgh.pa.us>
Permalink:  ../3054299.1677979338@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/3054299.1677979338@sss.pgh.pa.us

 · 

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: tgl@sss.pgh.pa.us, ronljohnsonjr@gmail.com
  Subject: Re: psql \set variables in crosstab queries?
  In-Reply-To: <3054299.1677979338@sss.pgh.pa.us>
  References: <1337b328-44f7-7f5b-cc1d-b8d11aced2cd@gmail.com> <3054299.1677979338@sss.pgh.pa.us>

* 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