Search code examples
batch-filesqlcmd

Check if sqlcmd was successful


I am using sqlcmd to run an sql query in a batch file. I would then like to – depending on if the sql query has been successfuly executed or not – do something.

sqlcmd -some flags -Q "Query"
iF successful (
    do this
) ELSE (
    do that
)

Thanks – I am new to batch programming by the way.


Solution

  • The exit code from a program is represented by the ERRORLEVEL environment variable. Use if /? for more details.

    sqlcmd -some flags -Q "Query"
    if ERRORLEVEL 1 (
        do this failed
    ) ELSE (
        do that success
    )