Quantcast
Channel: Forum Getting started with SQL Server
Viewing all articles
Browse latest Browse all 4729

sqlcmd error capture and if statement is not working

$
0
0
I am using below batch script to trigger the Job ABC.Job ABC is failed  but the if statement is not working.It should go to ErrorHandler block but it is going to.:Done block.Any help will be appreciated.

cd C:\Program Files\Microsoft SQL Server\110\Tools\Binn
sqlcmd -S xxxxxx -U xxxxxx -P xxxxxx  -b -Q "DECLARE @flag int; EXEC @flag = msdb.dbo.sp_start_job N'ABC'; IF @flag = 1 BEGIN select 'failure'; END ELSE BEGIN PRINT 'success'; END;" 
IF %ERRORLEVEL% == 0 GOTO Done
GOTO ErrorHandler
ECHO SQLCMD returned a zero exit code
:ErrorHandler
ECHO SQLCMD returned an error
:Done

Viewing all articles
Browse latest Browse all 4729

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>