I'm trying to use sql*plus to control a small Windows command script.
Basically, I want to execute some PL/SQL (perhaps select from a view or table or execute a function) which shows me the status of some rows in the database, and then depending upon the state of the rows, perform some Windows commands.
My problem is how to get the results back into the command script.
sqlplus user/password@server @script.sql
IF <CONDITIONAL HERE BASED on script.sql results> GOTO :runprocess
REM log and email that process had to be skipped
EXIT
:runprocess
REM run various Windows service commands
I encourage you to take a look at the two scripts included in the Oracle XE for backup and restore. These scripts have taught me a lot how to handle batch-scripting and Oracle on the Windows platform.
I would strongly encourage you to not use .bat files. You've got lots of other alternatives: C/C++ or VB, Windows scripting or Powershell, or even free downloads like Perl or Bash.
But here's one example of returning error codes in .bat files:
But please do look at some of the links I gave above. Avoiding .bat files will make it easier for you, and make it easier to maintain in the future.
IMHO ...
I do something like this by creating a .bat file which does the windows stuff and calling sql scripts as needed. Use SQL to spool your results to a text file which you can read.
In the sql use this command spool C:\yourResults.txt or for more sophisticated usages create a procedure, which, when called, writes the results to a text file using UTL_FILE
I'd probably write the script (or the conditional, depending on the requirements) from the called
script.sql
itself.For example, the following
script.sql
creates a .bat filewindows_commands.bat
:You can then call
script.sql
from yet another .bat file like so:This is what I ended up using.
My .cmd script:
Where script.sql:
Connected to: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0 - 64bit Production With the Partitioning, OLAP, Data Mining and Real Application Testing options