Is there some way to stop the Changed database context to ...
message when the piece of SQL has a USE database
in it ?
You need to set the errorlevel of sqlcmd
, which defaults to 0. Note: don't confuse the errorlevel here with the exit code of sqlcmd
that is returned to, say, cmd.exe
as the ERRORLEVEL
.
To disable this message for all of an sqlcmd
session, use the -m
commandline option:
sqlcmd -m 1 <other options>
To disable this message for a block of code, use the :setvar
batch command:
USE [mydb]
GO
-- Disable message for next USE command
:setvar SQLCMDERRORLEVEL 1
USE [mydb]
GO
-- Reenable
:setvar SQLCMDERRORLEVEL 0
...
To use the :setvar
(or other SQLCMD batch commands) in Management Studio, you need to enable the SQLCMD mode for the query window you're in (menu "Query / SQLCMD Mode"). You'll see that it is enabled, when lines starting with ':' have a gray background.