We generate a temporary script file (e.g. from the BATCH file) which contains:
DECLARE @intErrNo int
EXEC @intErrNo=dbo.MySProc @Param1=xxx, @Param2=yyy
:EXIT(SELECT @intErrNo)
The BATCH file contains:
SQLCMD -d MyDatabase -i MyTempScript.SQL -o X:\MyPath\MyProcessName.OUT -b -l 300 -S . -E >>X:\MyPath\MyProcessName.ERR
IF ERRORLEVEL 1 GOTO SQL1_Error
ECHO %DATE% %TIME% SQL MyProcessName END >>X:\MyPath\MyProcessName.ERR
GOTO SQL1_DONE
:SQL1_Error
ECHO %DATE% %TIME% SQL MyProcessName *** ERROR EXIT *** : %1 >>X:\MyPath\MyProcessName.ERR
ECHO ---------- >>X:\MyPath\MyProcessName.ERR
... Some stuff here to send an EMail with the error file attached ...
GOTO SQL1_DONE
:SQL1_DONE
... rest of batch file ...