Perhaps the following link related to your problem
using SP_SEND_DBMAIL, I just needed to use the parameter "@Execute_query_database = XXXXX";
or pass the -m flag as indicated here
check SQLCMDERRORLEVEL and the -m flag passed to sqlcmd.
C:\>sqlcmd -Q "use master" -E
Changed database context to 'master'.
C:\>sqlcmd -Q "use master" -E -m 1
C:\>
source
share