SQLCMD utility from BAT file - how to return ERRORLEVEL in case of syntax error
You do need the
-b switch but together with
enabledelayedexpansion, that way you can use
!errorlevel! inside the loop and get the expected results.
setlocal enabledelayedexpansion anywhere before you execute
sqlcmd, probably best at the beginning of the batch or just before the loop. Also note the use of exclamation points (
!) instead of the percent signs (
%), which denote the use of delayed expansion.
[I also tested with
if not errorlevel 0 … (no
!, nor any
help if) but I could not get the desired results]