From: | Adrian Klaver <adrian(dot)klaver(at)aklaver(dot)com> |
---|---|
To: | Ron <ronljohnsonjr(at)gmail(dot)com>, pgsql-general(at)lists(dot)postgresql(dot)org |
Subject: | Re: Syntax error when combining --set and --command has me stumped |
Date: | 2022-07-28 23:36:13 |
Message-ID: | 4299f8dc-7d37-b88c-de12-ac6830d3e10a@aklaver.com |
Views: | Raw Message | Whole Thread | Download mbox | Resend email |
Thread: | |
Lists: | pgsql-general |
On 7/28/22 12:40, Ron wrote:
>
> $ alias psql12
> alias psql12='/usr/lib/postgresql/12/bin/psql -p5433'
>
> This works ask expected:
>
> $ psql12 --set num=42 -ac "\echo :num"
> echo :num
> 42
>
> And so does this:
>
> $ psql12 --set num=42
> psql (12.11 (Ubuntu 12.11-1.pgdg18.04+1))
> Type "help" for help.
>
> postgres=# select :num;
> ?column?
> ----------
> 42
> (1 row)
>
>
> But trying to use a variable (both with and without single quotes) in a
> --command statement other than "\echo" throws a syntax error at the colon:
>
> $ psql12 --set num=42 -ac "select :num;"
> select :num;
> ERROR: syntax error at or near ":"
> LINE 1: select :num;
> ^
> $ psql12 --set num=42 -ac "select :'num';"
> select :'num';
> ERROR: syntax error at or near ":"
> LINE 1: select :'num';
> ^
>
> What secret sauce am I missing to get this to work?
From here:
https://www.postgresql.org/docs/current/app-psql.html
-c command
...
Because of this behavior, putting more than one SQL command in a single
-c string often has unexpected results. It's better to use repeated -c
commands or feed multiple commands to psql's standard input, either
using echo as illustrated above, or via a shell here-document, for example:
psql <<EOF
\x
SELECT * FROM foo;
EOF
So:
echo '\set num 42 \\ SELECT :num;' | psql -d test -U aklaver
Null display is "NULL".
?column?
----------
42
or:
psql -d test -U aklaver <<EOF
> \set num 42
> SELECT :num;
> EOF
Null display is "NULL".
?column?
----------
42
(1 row)
--
Adrian Klaver
adrian(dot)klaver(at)aklaver(dot)com
From | Date | Subject | |
---|---|---|---|
Next Message | Bryn Llewellyn | 2022-07-29 01:04:29 | « The PL/pgSQL interpreter parses the function's source text and produces an internal binary instruction tree... » |
Previous Message | Ron | 2022-07-28 19:50:41 | Re: Syntax error when combining --set and --command has me stumped |