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

Use batch file to run scripts one at a time using conditional statement

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

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 等等。

有什么说法可以帮助解决这个问题吗?

您可以使用 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