Re: psql syntax for array of strings in a variable?

From: Pavel Stehule <pavel(dot)stehule(at)gmail(dot)com>
To: Philip Semanchuk <philip(at)americanefficient(dot)com>
Cc: PostgreSQL General <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: psql syntax for array of strings in a variable?
Date: 2021-10-29 17:52:30
Message-ID: CAFj8pRAHUTg0ANbUTG_54EkEj3aC0L0Rm-dLya4J2mWsfu_geA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi

pá 29. 10. 2021 v 19:21 odesílatel Philip Semanchuk <
philip(at)americanefficient(dot)com> napsal:

> Hi,
> I would appreciate help with the syntax for querying an array of strings
> declared as a psql variable. Here's an example.
>
> \set important_days ARRAY['monday', 'friday']
>
> select 1 where 'monday' = ANY(:important_days);
> ERROR: 42703: column "monday" does not exist
> LINE 1: select 1 where 'monday' = ANY(ARRAY[monday,friday]);
>
> select 1 where 'monday' = ANY(:"important_days");
> ERROR: 42703: column "ARRAY[monday,friday]" does not exist
> LINE 1: select 1 where 'monday' = ANY("ARRAY[monday,friday]");
>
> I'm doing something wrong but I can't figure out what.
>
> My real-world use case is that I have a psql script that will execute
> several queries on a long list of strings, and rather than repeat those
> strings over and over in the script, I'd like to declare them once at the
> top of the script and then refer to the variable after that. Bonus points
> if there's a way to do a multiline declaration like --
>
> \set important_days ARRAY['monday',
> 'friday']
>
> Thanks for reading
> Philip
>

psql variables can hold only text. There is not any type - all is just text.

Regards

Pavel

In response to

Browse pgsql-general by date

  From Date Subject
Next Message Tom Lane 2021-10-29 18:05:24 Re: psql syntax for array of strings in a variable?
Previous Message David G. Johnston 2021-10-29 17:34:42 Re: psql syntax for array of strings in a variable?