passing string/date variables from shell to sql file
Posted: Tue Dec 02, 2014 3:13 am
How can I pass variables that require wrapping in single quotes (i.e. strings and dates) to a sql file using vsql from a bash command? vsql parameter substitution works fine for types that don't require single quote wrapping (int, numeric) in the sql, but I haven't found any examples that demonstrate usage with a type requiring quotes.
var_test.sql:
select table_name from tables where table_name = :table_name;
command:
$ vsql -d my_db -h localhost -w my_pwd -f ./var_test.sql -v table_name=my_table_name -e
The example above passes the :table_name variable in from the command line, but the sql throws an error because the table name is not wrapped in single quotes. If I wrap the :table_name variable in single quotes, the parameter substitution breaks.
Thanks
var_test.sql:
select table_name from tables where table_name = :table_name;
command:
$ vsql -d my_db -h localhost -w my_pwd -f ./var_test.sql -v table_name=my_table_name -e
The example above passes the :table_name variable in from the command line, but the sql throws an error because the table name is not wrapped in single quotes. If I wrap the :table_name variable in single quotes, the parameter substitution breaks.
Thanks