我试图使用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
然后,您可以拨打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
这是我最终使用。
我.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
其中script.sql:
SELECT COUNT(*)
FROM table
WHERE criteria = 1;
exit
我会强烈建议您不要使用.bat文件。 你有很多其他的替代品:C / C ++或VB, Windows脚本编写或Powershell的 ,甚至免费下载喜欢的Perl或猛砸 。
但这里的返回在.bat文件的错误代码的一个例子:
- http://www.dbforums.com/oracle/1044496-sqlplus-return-value-help.html
但请不要看一些我上面给的链接。 避免.bat文件会让你更容易,并使其更容易在未来的维护。
恕我直言 ...
我通过创建一个.bat文件,其不窗户的东西,并根据需要调用SQL脚本做这样的事情。 使用SQL后台你的结果,你可以阅读的文本文件。
... DOS命令这里
SQLPLUS / NOLOG @C:\自卸车\ DropRecreateUsers.sql
SQLPLUS / NOLOG @C:\自卸车\ Cleanup.sql
... DOS命令
在SQL使用此命令阀芯C:\ yourResults.txt或更复杂的用法创建一个过程,它被调用时,将结果写入使用UTL_FILE一个文本文件
我鼓励你看看包含在Oracle XE两个脚本进行备份和恢复。 这些脚本已经教了我很多如何处理批处理脚本和Oracle在Windows平台上。
- C:\ oraclexe \程序\ ORACLE \产品\ 11.2.0 \服务器\ BIN \ BACKUP.BAT
- C:\ oraclexe \程序\ ORACLE \产品\ 11.2.0 \服务器\ BIN \ Restore.bat
连接到:Oracle数据库11g企业版发布11.2.0.1.0 - 64位生产有了分区,OLAP,数据挖掘和实时应用测试选项
SQL> @f:\testa.txt