On Sun, 2009-09-20 at 16:49 -0500, Alex Gadea wrote:
> I am using psql to call an external sql file that executes a simple
> select count(*):
>
> ie: select into ct count(*) from table;
>
> I can't figure out how to make the ct variable available to the shell
> script once the external sql file completes execution.
Hi Alex,
If you are using bash, for example:
COUNT=`psql -d <database> -Atc "select count(*) from table;"`
in a shell script, any string enclosed in back-quotes (`) is executed & the result is returned. So the above expression
assignsthe value returned by the psql command to the variable called COUNT. The -Atc tells psql to return only the
unalignedvalue, no formatting or column names, etc.
If you store your SQL command outside the script, then you could use:
COUNT=`psql -d <database> -Atf <SQL_file>`
HTH,
Brent Wood
Brent Wood
DBA/GIS consultant
NIWA, Wellington
New Zealand
NIWA is the trading name of the National Institute of Water & Atmospheric Research Ltd.