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 fun
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
SQL> @f:\testa.txt
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.
...dos commands here
sqlplus /nolog @C:\Dump\DropRecreateUsers.sql
sqlplus /nolog @C:\Dump\Cleanup.sql
...dos commands
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 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 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.
This is what I ended up using.
My .cmd script:
@ECHO OFF
ECHO Checking Oracle...
for /f %%i in ('sqlplus -s user/password@database @script.sql') do @set count=%%i
echo %count%
IF %count% GTR 0 GOTO :skipped
GOTO :runprocess
Where script.sql:
SELECT COUNT(*)
FROM table
WHERE criteria = 1;
exit
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 file windows_commands.bat
:
set feedback off
set echo off
set trimspool on
set termout off
set serveroutput on size 100000 format wrapped
set lines 500
set pages 0
-- create the bat file to be executed later:
spool windows_commands.bat
declare
c number;
begin
select count(*) into c from dual;
-- depending on a conditional, write the stuff to be executed into the
-- bat file (windows_commands.bat)
if c = 1 then
dbms_output.put_line('@echo everthing ok with dual');
else
dbms_output.put_line('@echo something terribly wrong with dual');
end if;
end;
/
spool off
exit
You can then call script.sql
from yet another .bat file like so:
@rem create oracle session, call script.sql
sqlplus %user%/%password%@%db% @script.sql
@rem script.sql has created windows_commands.bat.
@rem call this newly created bat file:
call windows_commands.bat