Обсуждение: psql command line variables are unknown when -c SQL statement are executed

Поиск
Список
Период
Сортировка

psql command line variables are unknown when -c SQL statement are executed

От
Pavel Stehule
Дата:
wrong:

pavel@nemesis ~]$ psql postgres -v x=3D10 -c "select :x"
ERROR:  syntax error at or near ":"
LINE 1: select :x
               ^

good (workaround):
[pavel@nemesis ~]$ echo 'select :x' | psql postgres -v x=3D10
 ?column?
=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=94=80=E2=
=94=80=E2=94=80
       10
(1 row)

psql have to have process external variables first and then execute query.

Regards
Pavel Stehule

Re: psql command line variables are unknown when -c SQL statement are executed

От
Peter Eisentraut
Дата:
On mån, 2009-11-16 at 10:19 +0100, Pavel Stehule wrote:
> wrong:
>
> pavel@nemesis ~]$ psql postgres -v x=10 -c "select :x"
> ERROR:  syntax error at or near ":"
> LINE 1: select :x
>                ^

This is documented in the psql man page.

Re: psql command line variables are unknown when -c SQL statement are executed

От
Pavel Stehule
Дата:
2009/11/16 Peter Eisentraut <peter_e@gmx.net>:
> On m=C3=A5n, 2009-11-16 at 10:19 +0100, Pavel Stehule wrote:
>> wrong:
>>
>> pavel@nemesis ~]$ psql postgres -v x=3D10 -c "select :x"
>> ERROR: =C2=A0syntax error at or near ":"
>> LINE 1: select :x
>> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0^
>
> This is documented in the psql man page.

I don't see it, Peter?

Is it some reason for it? I don't understand, why this order is correct:

execute statement
process external variables
finish

Pavel

>
>
>

Re: psql command line variables are unknown when -c SQL statement are executed

От
Peter Eisentraut
Дата:
On mån, 2009-11-16 at 12:28 +0100, Pavel Stehule wrote:
> 2009/11/16 Peter Eisentraut <peter_e@gmx.net>:
> > On mån, 2009-11-16 at 10:19 +0100, Pavel Stehule wrote:
> >> wrong:
> >>
> >> pavel@nemesis ~]$ psql postgres -v x=10 -c "select :x"
> >> ERROR:  syntax error at or near ":"
> >> LINE 1: select :x
> >>                ^
> >
> > This is documented in the psql man page.
>
> I don't see it, Peter?

       -c command

       --command command
              Specifies that psql is to execute one command string,
command, and then exit. This is useful in shell scripts.

              command  must  be  either a command string that is
completely parsable by the server (i.e., it contains no psql specific
features), or a single backslash command. Thus you cannot
              mix SQL and psql meta-commands with this option.

> Is it some reason for it? I don't understand, why this order is correct:
>
> execute statement
> process external variables
> finish

Well, -c works a bit different so that it is possible at all to send a
command to the server without any psql processing in the way.  It's a
poor excuse, from a user's point of view, but that's historically why
it's been kept that way.

Re: psql command line variables are unknown when -c SQL statement are executed

От
Pavel Stehule
Дата:
2009/11/18 Peter Eisentraut <peter_e@gmx.net>:
> On m=C3=A5n, 2009-11-16 at 12:28 +0100, Pavel Stehule wrote:
>> 2009/11/16 Peter Eisentraut <peter_e@gmx.net>:
>> > On m=C3=A5n, 2009-11-16 at 10:19 +0100, Pavel Stehule wrote:
>> >> wrong:
>> >>
>> >> pavel@nemesis ~]$ psql postgres -v x=3D10 -c "select :x"
>> >> ERROR: =C2=A0syntax error at or near ":"
>> >> LINE 1: select :x
>> >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0^
>> >
>> > This is documented in the psql man page.
>>
>> I don't see it, Peter?
>
> =C2=A0 =C2=A0 =C2=A0 -c command
>
> =C2=A0 =C2=A0 =C2=A0 --command command
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Specifies that psql is to=
 execute one command string,
> command, and then exit. This is useful in shell scripts.
>
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0command =C2=A0must =C2=A0=
be =C2=A0either a command string that is
> completely parsable by the server (i.e., it contains no psql specific
> features), or a single backslash command. Thus you cannot
> =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0mix SQL and psql meta-com=
mands with this option.
>
>> Is it some reason for it? I don't understand, why this order is correct:
>>
>> execute statement
>> process external variables
>> finish
>
> Well, -c works a bit different so that it is possible at all to send a
> command to the server without any psql processing in the way. =C2=A0It's a
> poor excuse, from a user's point of view, but that's historically why
> it's been kept that way.

ok - thank you. I could to live with it.

Regards
Pavel Stehule
>
>