我正在尝试编写一个简单的批处理脚本,我们的用户可以使用该脚本将一些代码重新部署到环境中,前提是该环境最近已经刷新并清除了我们的更改。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命令。
这是'echo exit | sqlplus -L -S%DB_USER%/%DB_PASS%@%DB%@ %%每次你引用“调用login.sql”。这就是它的工作原理。 – Guru
您是否确认脚本不包含SET ECHO OFF? – dbenham
@dbenham是的,我写了脚本。它们包含的唯一非DDL/DML是ALTER SESSION命令。 – sukach