Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pYd52-0005TI-0G for pgsql-general@arkaria.postgresql.org; Sun, 05 Mar 2023 01:22:28 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pYd4z-00059a-AM for pgsql-general@arkaria.postgresql.org; Sun, 05 Mar 2023 01:22:25 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pYd4y-00059H-W1 for pgsql-general@lists.postgresql.org; Sun, 05 Mar 2023 01:22:24 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pYd4v-0006PE-Dy for pgsql-general@lists.postgresql.org; Sun, 05 Mar 2023 01:22:24 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 3251MIV03054300; Sat, 4 Mar 2023 20:22:18 -0500 From: Tom Lane To: Ron cc: pgsql-general@lists.postgresql.org Subject: Re: psql \set variables in crosstab queries? In-reply-to: <1337b328-44f7-7f5b-cc1d-b8d11aced2cd@gmail.com> References: <1337b328-44f7-7f5b-cc1d-b8d11aced2cd@gmail.com> Comments: In-reply-to Ron message dated "Sat, 04 Mar 2023 18:20:29 -0600" MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-ID: <3054298.1677979338.1@sss.pgh.pa.us> Content-Transfer-Encoding: 8bit Date: Sat, 04 Mar 2023 20:22:18 -0500 Message-ID: <3054299.1677979338@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Ron 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