Re: psql client: technique for applying default values to :variables? - Mailing list pgsql-sql

From Andreas Seltenreich
Subject Re: psql client: technique for applying default values to :variables?
Date
Msg-id 87psmsumvy.fsf@gate450.dyndns.org
Whole thread Raw
In response to Re: psql client: technique for applying default values to :variables?  (Jeff Boes <jeff@endpoint.com>)
List pgsql-sql
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
-- 


pgsql-sql by date:

Previous
From: Ottó Havasvölgyi
Date:
Subject: For update
Next
From: Alvaro Herrera
Date:
Subject: Re: For update