Re: psql variable interpolation with subsequent underscore

From: Adrian Klaver <adrian(dot)klaver(at)gmail(dot)com>
To: Tim Kane <tim(dot)kane(at)gmail(dot)com>, pgsql-general General <pgsql-general(at)postgresql(dot)org>
Subject: Re: psql variable interpolation with subsequent underscore
Date: 2013-11-08 16:47:13
Message-ID: 527D1591.8030308@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On 11/08/2013 08:08 AM, Tim Kane wrote:
> Hi all,
>
> I’m having difficulty using variable interpolation within psql, where
> that variable is within a table name…
>
>
> Like so..
>
> =# set YYYYMM 201310
> =# select :YYYYMM;
> ?column?
> ----------
> 201309
> (1 row)
>
>
> =# alter table my_table rename to my_table_:YYYYMM_raw;
> ERROR: syntax error at or near ":"
> LINE 1: …my_table rename to my_table_:YYYYMM_ra...
> ^
>
> The problem is that psql tries to interpret ‘YYYYMM_raw’ as the variable
> name, but my intention is to only interpret ‘YYYYMM’ followed by a
> literal underscore.
>
> I can’t find any other way to encapsulate the variable name in this way…
> Is there a trick to it?
>
> I suspect I’ll need to work around this by altering the naming
> convention such that the YYYYMM is at the ‘end’ of the table name.
> Maybe.. Thoughts?
>

test=> \set tbl_name 'my_table_':YYYYMM'_raw';

test=> alter table my_table rename to :tbl_name;
ALTER TABLE

test=> \d my_table_201310_raw
Table "public.my_table_201310_raw"
Column | Type | Modifiers
--------+---------+-----------
id | integer |

>
> Tim
>
>
>

--
Adrian Klaver
adrian(dot)klaver(at)gmail(dot)com

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Tim Kane 2013-11-08 17:30:30 Re: psql variable interpolation with subsequent underscore
Previous Message Tom Lane 2013-11-08 16:40:43 Re: Strange result with "SELECT ... ORDER BY random() LIMIT 1" and JOINs