如何在Windows命令脚本中使用sql*plus来控制流量?

Cad*_*oux 8 windows oracle sqlplus batch-file oracle10g

我正在尝试使用sql*plus来控制一个小的Windows命令脚本.

基本上,我想执行一些PL/SQL(可能从视图或表中选择或执行一个函数),它向我显示数据库中某些行的状态,然后根据行的状态执行一些Windows命令.

我的问题是如何将结果返回到命令脚本中.

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
Run Code Online (Sandbox Code Playgroud)

Ren*_*ger 7

我可能会从被调用script.sql本身编写脚本(或条件,取决于要求).

例如,以下内容script.sql创建.bat文件 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
Run Code Online (Sandbox Code Playgroud)

然后你可以script.sql从另一个.bat文件调用,如下所示:

@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
Run Code Online (Sandbox Code Playgroud)


Cad*_*oux 5

这就是我最终使用的.

我的.cmd脚本:

@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
Run Code Online (Sandbox Code Playgroud)

其中script.sql:

SELECT COUNT(*)
FROM table
WHERE criteria = 1;

exit
Run Code Online (Sandbox Code Playgroud)