psql client: technique for applying default values to :variables?
psql client: technique for applying default values to :variables?
От:
Jeff Boes <jeff@endpoint.com>
Дата:
Stumped: is there any way to set up default values for psql variables
within the .SQL file itself? Obviously, I can do something like:
$ psql -f my_script -v MYVAR=${myvar:-mydefault}
but I would prefer to have the value stored with the .SQL file, e.g. (if
this actually worked):
\set MYVAR COALESCE(:MYVAR,'mydefault')
--
Jeffery Boes <><
jeff@endpoint.com
Re: psql client: technique for applying default values to :variables?
От:
Tony Wasson <ajwasson@gmail.com>
Дата:
On 1/11/06, Jeff Boes wrote:
> Stumped: is there any way to set up default values for psql variables
> within the .SQL file itself? Obviously, I can do something like:
>
> $ psql -f my_script -v MYVAR=${myvar:-mydefault}
>
> but I would prefer to have the value stored with the .SQL file, e.g. (if
> this actually worked):
>
> \set MYVAR COALESCE(:MYVAR,'mydefault')
Stuff like this works for me in a SQL file....
\set edate 'CURRENT_DATE::DATE'
SELECT * FROM some_table WHERE update_date = :edate;
Re: psql client: technique for applying default values to :variables?
От:
Jeff Boes <jeff@endpoint.com>
Дата:
Tony Wasson wrote:On 1/11/06, Jeff Boes <jeff@endpoint.com> wrote:Stumped: is there any way to set up default values for psql variables within the .SQL file itself? Obviously, I can do something like: $ psql -f my_script -v MYVAR=${myvar:-mydefault} but I would prefer to have the value stored with the .SQL file, e.g. (if this actually worked): \set MYVAR COALESCE(:MYVAR,'mydefault')Stuff like this works for me in a SQL file.... \set edate 'CURRENT_DATE::DATE' SELECT * FROM some_table WHERE update_date = :edate;
Sure, but that was not my question. I want to be able to set the variable on the command line, BUT have it default to a value inside the SQL script if not present on the command line.
$ psql -v edate=2004-01-01
...
=> \set edate 'CURRENT_DATE::DATE'
=> select :edate;
date
------------
2006-01-14-- Jeffery Boes <>< jeff@endpoint.com
Re: psql client: technique for applying default values to :variables?
От:
Michael Glaesemann <grzm@myrealbox.com>
Дата:
On Jan 14, 2006, at 23:54 , Jeff Boes wrote: > Tony Wasson wrote: >> Stuff like this works for me in a SQL file.... \set edate >> 'CURRENT_DATE::DATE' SELECT * FROM some_table WHERE update_date >> = :edate; > > Sure, but that was not my question. I want to be able to set the > variable on the command line, BUT have it default to a value inside > the SQL script if not present on the command line. You could write a wrapper script that would accept a command line argument and load the file with the appropriate SET statement prepended. Michael Glaesemann grzm myrealbox com
Re: psql client: technique for applying default values to :variables?
От:
Jeff Boes <jeff@endpoint.com>
Дата:
Michael Glaesemann wrote:
>
> On Jan 14, 2006, at 23:54 , Jeff Boes wrote:
>
>> Tony Wasson wrote:
>
>>
>> Sure, but that was not my question. I want to be able to set the
>> variable on the command line, BUT have it default to a value inside
>> the SQL script if not present on the command line.
>
> You could write a wrapper script that would accept a command line
> argument and load the file with the appropriate SET statement prepended.
Which really gains me nothing over just:
$ psql -f myscript.sql -v "MYVAR=${myvar:-default}"
The original question was: how can I store the default value in the
"myscript.sql" script where it's used, rather than have it in a calling
script? The aim here was to put the default in the code, so they would
be bound together...
--
Jeffery Boes <><
jeff@endpoint.com
Re: psql client: technique for applying default values to :variables?
От:
Andreas Seltenreich <andreas+pg@gate450.dyndns.org>
Дата:
Jeff Boes writes:
> Michael Glaesemann wrote:
>> You could write a wrapper script that would accept a command line
>> argument and load the file with the appropriate SET statement prepended.
>
> Which really gains me nothing over just:
>
> $ psql -f myscript.sql -v "MYVAR=${myvar:-default}"
>
> The original question was: how can I store the default value in the
> "myscript.sql" script where it's used, rather than have it in a calling
> script? The aim here was to put the default in the code, so they would
> be bound together...
How about using the traditional Unix macro processor "m4" instead of
psql's variables?
E.g., you could write a myscript.sql.m4 file with m4 variables instead
of psql ones. You could then use ifdef() to test for previous
definitions, or even define a default()-wrapper macro.
regards,
Andreas
--