我正在尝试使用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

最佳答案

我可能会从调用的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

然后可以从另一个.bat文件调用script.sql,如下所示:
@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

07-24 09:46
查看更多