使用批处理文件使用条件语句一次运行一个脚本

时间:2017-03-22 10:27:19

标签: windows oracle batch-file cmd sqlplus

我有许多脚本保存在一个文件夹中,并且基于层次结构一次一个地运行一个批处理文件。我开发了简单的批处理文件;

sqlplus username/password@database
@/apps/Batch/script_1.txt
mkdir Results
move "script_1.txt" Results

在运行结束时,script_1.txt将移动到另一个文件夹。 我有其他脚本 - script_2.txt,script_3.txt等 - 但我想有一个场景,运行批处理文件只按层次结构执行最前面的文件,即再次运行批处理文件将执行script_2.txt等等。

是否有任何声明可以帮助解决这个问题?

1 个答案:

答案 0 :(得分:0)

您可以使用FOR循环遍历目录中的所有文件,在SQLPlus中运行它们并将它们移动到其他文件夹。 例如,如果我有一个run.cmd,如下所示:

@echo off
for %%i in (*.sql) do (sqlplus siu/siu@middle_svil @%%i
IF NOT EXIST "Results" mkdir "Results"
move %%i Results
)

和一组3个SQL脚本script_1,script_2,script_3如下所示:

<强> script_1.sql:

set serveroutput on
prompt this is script 1
exit

<强> script_2.sql:

set serveroutput on
prompt this is script 2
exit

<强> script_3.sql:

set serveroutput on
prompt this is script 3
exit

这就是我在开始时所拥有的:

D:\test>dir

23/03/2017  09:28    <DIR>          .
23/03/2017  09:28    <DIR>          ..
23/03/2017  09:28    <DIR>          Results
23/03/2017  09:24               127 run.cmd
22/03/2017  14:09                50 script_1.sql
22/03/2017  14:08                50 script_2.sql
22/03/2017  14:13                50 script_3.sql
               4 File            277 byte

我运行脚本:

D:\test>run

SQL*Plus: Release 11.2.0.2.0 Production on Gio Mar 23 09:28:34 2017

Copyright (c) 1982, 2014, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

this is script 1
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
        1 file spostato/i.

SQL*Plus: Release 11.2.0.2.0 Production on Gio Mar 23 09:28:35 2017

Copyright (c) 1982, 2014, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

this is script 2
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
        1 file spostato/i.

SQL*Plus: Release 11.2.0.2.0 Production on Gio Mar 23 09:28:36 2017

Copyright (c) 1982, 2014, Oracle.  All rights reserved.


Connected to:
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options

this is script 3
Disconnected from Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
With the Partitioning, OLAP, Data Mining and Real Application Testing options
        1 file spostato/i.

如您所见,3 sql脚本已运行,文件已在新目录中移动。

事实上:

D:\test>dir


23/03/2017  09:28    <DIR>          .
23/03/2017  09:28    <DIR>          ..
23/03/2017  09:28    <DIR>          Results
23/03/2017  09:24               127 run.cmd
               1 File            127 byte

D:\test>cd results

D:\test\Results>dir

23/03/2017  09:28    <DIR>          .
23/03/2017  09:28    <DIR>          ..
22/03/2017  14:09                50 script_1.sql
22/03/2017  14:08                50 script_2.sql
22/03/2017  14:13                50 script_3.sql
               3 File            150 byte

D:\test\Results>

如果您需要一次只运行一个脚本,可以在脚本中添加GOTO,以便在运行sql脚本后退出:

@echo off
for %%i in (*.sql) do (sqlplus siu/siu@middle_svil @%%i
IF NOT EXIST "Results" mkdir "Results"
move %%i Results
goto :end
)
:end