4

Is it possible to execute an sql file (a text file containing several SQL statements) against an oracle database from a bash shell command line (e.g. no graphical GUI and no interactive clients)

Something like: sqlplus -f queries.sql ...

The command is expected to execute all the statements in the queries.sql file and exit with an appropriate exit code (e.g. 0 if all queries executed correctly, nonzero otherwise)

asked Jun 28, 2012 at 15:00

1 Answer 1

5

In a shell script:

#!/bin/bash
sqlplus user/pass@server/DATABASE<<THEEND
-- Change "1" to the desired fatal return code
whenever sqlerror exit 1;
@yoursqlscript.sql
quit;
THEEND

Or you can just run:

sqlplus user/pass@server/DATABASE @yoursqlscript

... and put the whenever sqlerror exit 1; at the top of your .sql script(s).

a1an
1571 silver badge7 bronze badges
answered Jun 28, 2012 at 15:05
2
  • 1
    +1, you should also use the -L option so that sqlplus will return immediately with an error if it fails to login. Commented Jun 28, 2012 at 15:17
  • The Or part of your answer, with sqlplus user/pass@server/DATABASE @yoursqlscript works putting the statement (with an ending ";") at the beginning of the script Commented Jun 28, 2012 at 15:58

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.