2012-05-14 4 views
0

사용자가 최근 환경을 새로 고치고 변경 사항을 지우는 경우 약간의 코드를 환경에 재배포하는 데 사용할 수있는 간단한 배치 스크립트를 작성하려고합니다.Windows 배치 파일을 통해 실행할 때 ECHO가 올바르게 작동하지 않습니다.

스크립트는 훌륭하게 작동하지만 원하는대로 로깅하지 않습니다. 배포 과정에서 제한 사항을 수행합니다 (저는 DBA가 아니며 단지 개발자). ECHO 명령을 파일에 포함 할 수 없습니다.

배치 스크립트의 일부로이 문제를 해결하려면 작업 디렉토리에 "SET ECHO ON"및 "SPOOL file.log APPEND"명령이 포함 된 login.sql 파일을 만듭니다. 목표는 스크립트 전에 실행되어 내 결과를 스풀링한다는 것입니다. 그러나 SPOOL 명령이 작동하는 것처럼 보이지만 스크립트의 내용은 ECHO되지 않습니다. 이 모든 별도의 스크립트와 별도의 SQL * Plus 세션을 시작하기 때문에

:: 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 
) 

나의 이해, 그것은 login.sql를 파일마다 호출되어있다 : 여기

는 배치 스크립트에서 관련 코드 매번 SET ECHO ON 명령을 사용해야합니다.

+0

이것은'echo exit | sqlplus -L -S % DB_USER %/% DB_PASS % @ % DB % @ %% i' 당신은 매번 login.sql을 호출하는 것으로 언급하고 있습니다. 그것이 작동하는 방법입니다. – Guru

+0

스크립트에 SET ECHO OFF가 포함되어 있지 않음을 확인 했습니까? – dbenham

+0

@dbenham 예, 스크립트를 작성했습니다. 유일한 DDL/DML은 ALTER SESSION 명령입니다. – sukach

답변

0

BAT 파일에서 사용하는 경우 ECHO은 환경 변수가 아니라 명령임을 기억하십시오. 에코를 켜는 명령은 ECHO ON입니다. SET ECHO ON은 에코 동작에 영향을주지 않습니다. "환경 변수 ECHO가 정의되지 않았습니다."을 대신 반환합니다.

+0

@Andriy 당신이 옳다. 편집 해 주셔서 감사합니다. –

+0

SET ECHO ON은 SQL * Plus 명령입니다. 스크립트를 실행하기 전에 login.sql 파일에이 명령을 쓰려고합니다. – sukach

관련 문제