Welcome to EMC Consulting Blogs Sign in | Join | Help

Merrick Chaffer's Blog

Returning an ERRORLEVEL to DOS prompt from OSQL or SQLCMD utility

After much trial and tribulation today I've finally cracked how to return a non zero exit code from a call to osql back to a dos batch file.

The answer is as follows. Note the bits in bold below are most important, and the use of the single & sign to denote running the IF ERRORLEVEL 1 command even if the previous osql command errors.

 

for /F %%i in ('dir *.sql /b') do osql -b -E -S MyServer\InstanceName -d MyDatabase -i %%i & IF ERRORLEVEL 1 goto ABORTBATCH

:ABORTBATCH
popd
@echo on
REM Specify non zero exit code to signal error occurred
exit /b 1

Published 26 November 2007 10:59 by merrick.chaffer
Filed under: , , , ,

Comments

 

jamie.thomson said:

What no Powershell? :)

November 26, 2007 14:35
Anonymous comments are disabled

This Blog

Syndication

News

Powered by Community Server (Personal Edition), by Telligent Systems