Execute Oracle Query via unix shell sql plus, query is in file

Hello experts
I want to execute a sql query from a shell script. I have the commands in my .sh script:
sqlplus user/pw@db
connnect user/ow@db
spool myres
->This all works well, now I want to execute a sql command which is in a file:
@myquery.sql
->I get SP2-0734: unknown command beginning "@myquery ..." rest of the line ignored

How can I in my script exectute a SQL query which is in a file?
mruffAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Wasim Akram ShaikCommented:
You need to show us little bit more to assist you further.. may be the code which you are trying to execute in myquery.sql has blank lines.. this is what i could think of with the error associated with SP2-0734

try adding this line in the very beginning of the file myquery.sql

set sqlblanklines on

Open in new window

and try to re-execute the code in similar fashion you have posted here...
if you still get an error, post the code of the file myquery.sql
0
Gerwin Jansen, EE MVETopic Advisor Commented:
Just put the query as a parameter on the sql prompt (add paths if necessary):
sqlplus -s ${user}/${pw}@${db} @myquery.sql >> ${output}

Open in new window

The above will run the query and send it's output to ${output}, just define a few variables for user, pw, db and output.

You don't need the connect statement in your sql file, you are already connecting using sqlplus.
0
slightwv (䄆 Netminder) Commented:
I prefer to NOT place the username and password on the command line.  It is a huge security issue.

Try a HERE doc:

sqlplus << EOF
connnect user/ow@db
spool myres
@myquery.sql
exit
spool off
EOF
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Oracle Database

From novice to tech pro — start learning today.