2012-05-14 187 views
0

我正在尝试编写一个简单的批处理脚本,我们的用户可以使用该脚本将一些代码重新部署到环境中,前提是该环境最近已经刷新并清除了我们的更改。ECHO在通过Windows批处理文件执行时无法正常工作

这个脚本很好用,但它没有按照我想要的方式记录。做我们的部署过程(我不是DBA,只是一个开发人员)的限制,我们不能在文件中包含ECHO命令。

为了解决这个问题,作为批处理脚本的一部分,我在包含“SET ECHO ON”和“SPOOL file.log APPEND”命令的工作目录中创建了一个login.sql文件。目标是在脚本和假脱机输出之前执行此操作。但是,当SPOOL命令似乎正在工作时,脚本的内容不会被ECHOed。

下面是批处理脚本的相关代码:

:: Identify all files that begin with a number and end with .sql and write to a file. 
dir /b *.sql | findstr /b "[0-9]" > ScriptsToImplement.txt 

:: Show the user the files to be executed and confirm whether to proceed 
echo The following scripts will be executed in %DB%: 
for /f "delims=" %%i in (ScriptsToImplement.txt) do echo %%i 
echo: 
set /p PROCEED=Proceed with implementation? [y/n] ^> 
if /i "%PROCEED%" NEQ "y" (goto :opt_to_terminate) 

:: Build the user profile SQL file that will be executed after logging into SQLPlus 
:: This allows for the spooling of the output without having the SPOOL command declared in the file. 
echo SET ECHO ON >> login.sql 
echo SPOOL %DB%~%FOLDER%~%DATESTAMP%.log APPEND >> login.sql 

:: Go through the SQL files in the text file and execute them in the specified database. 
for /f "delims=" %%i in (ScriptsToImplement.txt) do (
echo exit | sqlplus -L -S %DB_USER%/%DB_PASS%@%DB% @%%i 
if %errorlevel% NEQ 0 (@echo an error occurred with applying %%i; stopping further script execution... & pause & goto :terminate) else (@echo %%i was applied successfully) >> %DATESTAMP%_run_all.log 
) 

我的理解是,因为这是启动一个单独的SQL * Plus会话的每个单独的脚本,它在调用每次的login.sql文件,所以应该每次都使用SET ECHO ON命令。

+0

这是'echo exit | sqlplus -L -S%DB_USER%/%DB_PASS%@%DB%@ %%每次你引用“调用login.sql”。这就是它的工作原理。 – Guru

+0

您是否确认脚本不包含SET ECHO OFF? – dbenham

+0

@dbenham是的,我写了脚本。它们包含的唯一非DDL/DML是ALTER SESSION命令。 – sukach

回答

0

如果您在BAT文件中使用它,请记住ECHO是一个命令,而不是环境变量。开启回声的命令是ECHO ONSET ECHO ON不影响回声行为。它返回而不是“环境变量ECHO未定义”

+0

@Andriy你说得对,编辑得更清楚,谢谢 –

+0

SET ECHO ON是一个SQL * Plus命令。我正在写这个命令给我的login.sql文件,期望它在我的脚本之前被执行。 – sukach

相关问题