问题
I'm trying to delete the database using sqlcmd with a batch script. The problem I was facing is the code isnot entering the errorhandler block if mytest database is not available in the sql.
@echo off
@echo IF ELSE DEMO
sqlcmd -S <servername> -U <username> -P <password> -Q "DROP DATABASE Mytest"
IF ERRORLEVEL 1 goto err_handler
goto done
:err_handler
echo handle the error here
goto end
:done
echo script completion code here
:end
@echo %Message%
pause
and it gives the following output:
IF ELSE DEMO
Msg 3701, Level 11, State 1, Server HERCULES, Line 1
Cannot drop the database 'Mytest', because it does not exist or you do not have permission.
script completion code here
ECHO is off.
Press any key to continue . . .
My expectation is:
if the database is available it should delete the database and should enter into :done code if the database is not available it should enter into :err_handler block.
I don't want Echo is off message.
回答1:
From MS documentation:
Error Reporting Options
-b
Specifies that sqlcmd exits and returns a DOS ERRORLEVEL value when an error occurs. The value that is returned to the DOS ERRORLEVEL variable is 1 when the SQL Server error message has a severity level greater than 10; otherwise, the value returned is 0. If the -V option has been set in addition to -b, sqlcmd will not report an error if the severity level is lower than the values set using -V. Command prompt batch files can test the value of ERRORLEVEL and handle the error appropriately. sqlcmd does not report errors for severity level 10 (informational messages).
If the sqlcmd script contains an incorrect comment, syntax error, or is missing a scripting variable, ERRORLEVEL returned is 1.
-m error_level
Controls which error messages are sent to stdout. Messages that have a severity level greater than or equal to this level are sent. When this value is set to -1, all messages including informational messages, are sent. Spaces are not allowed between the -m and -1. For example, -m-1 is valid, and -m -1 is not.
This option also sets the sqlcmd scripting variable SQLCMDERRORLEVEL. This variable has a default of 0.
-V error_severity_level
Controls the severity level that is used to set the ERRORLEVEL variable. Error messages that have severity levels greater than or equal to this value set ERRORLEVEL. Values that are less than 0 are reported as 0. Batch and CMD files can be used to test the value of the ERRORLEVEL variable.
You will need to use the adecuated values to get a error from the database. Without them, the errorlevel
you see is refered to the sqlcmd
instance executed (should be 0
as it has been sucessfully executed), not the command running in the database.
Also, as already commented, the ECHO is off.
message is generated by your
echo %Message%
with the variable undefined. The line is parsed and converted to
echo
as %Message%
has no value. And the behaviour of echo
command without parameters is to output the echo
state.
来源:https://stackoverflow.com/questions/29002920/errorlevel-in-batch-file-not-working