You can also pass parameters on the psql command line or from a batch file. The first instructions collect the necessary data to connect to your database.
The last prompt asks for the constraint values โโthat will be used in the WHERE column IN () clause. Do not forget to specify a single quote, if lines, and separated by a comma:
@echo off echo "Test for Passing Params to PGSQL" SET server=localhost SET /P server="Server [%server%]: " SET database=amedatamodel SET /P database="Database [%database%]: " SET port=5432 SET /P port="Port [%port%]: " SET username=postgres SET /P username="Username [%username%]: " SET /P bunos="Enter multiple constraint values for IN clause [%constraints%]: " ECHO you typed %constraints% PAUSE REM pause "C:\Program Files\PostgreSQL\9.0\bin\psql.exe" -h %server% -U %username% -d %database% -p %port% -e -v v1=%constraints% -f test.sql
Now in your SQL code file, add the v1 token to the WHERE clause or somewhere else in SQL. Note that tokens can also be used in an open SQL statement, and not just in a file. Save this as test.sql:
SELECT * FROM myTable WHERE NOT someColumn IN (:v1);
On Windows, save the entire file as a DOS BATch file (.bat), save test.sql in the same directory, and run the batch file.
Thanks for the Dave Page, EnterpriseDB, for the original script request.
MAbraham1 Apr 26 '12 at 16:28 2012-04-26 16:28
source share