How to execute an SQL statement stored in a shell script variable using isql

Rishabh picture Rishabh · Mar 7, 2012 · Viewed 27.5k times · Source

So, I am trying to execute an SQL statement stored in a shell script variable using isql

Generic code attached.

SQL="select * from Student"

EMPLOYEES=`isql -U $USERNAME -P $PASSWORD -D $DATABASE -S $SERVER<<EOF
$SQL
go

However on executing this, I get an error such as: Incorrect syntax near the keyword 'Select' and Incorrect suntax near '='

Anyone knows how to execute the SQL query directly stored in a variable from isql?

Answer

Ivan Kinash picture Ivan Kinash · Mar 7, 2012

Try in this way:

EMPLOYEES=`echo "$SQL" | isql -U $USERNAME -P $PASSWORD -D $DATABASE -S $SERVER`