SQLCMD utility from BAT file - how to return ERRORLEVEL in case of syntax error

后端 未结 2 1408
终归单人心
终归单人心 2021-02-08 11:48

How can I get %ERRORLEVEL% from SQLCMD utility when some of .sql files contains syntax error? These files create stored procedures. They don\'t invoke \"raiseerror\", but they c

2条回答
  •  谎友^
    谎友^ (楼主)
    2021-02-08 12:27

    Thank you, here is the work batch script.

    @ECHO OFF
    setlocal enabledelayedexpansion
    FOR /R "C:\SQL" %%G IN (*.sql) DO (
    sqlcmd -S%1 -d tangoDB -E -h-1 -w255 -i "%%G" -b
    echo   %%G  -  !ERRORLEVEL!
    IF !ERRORLEVEL! NEQ 0 EXIT /B !ERRORLEVEL!
    )
    

提交回复
热议问题