如何停止“将数据库上下文更改为..."信息 [英] How to stop the "Changed database context to ..." message
问题描述
当 SQL 片段中包含 USE 数据库
时,是否有某种方法可以阻止 Changed database context to ...
消息?
Is there some way to stop the Changed database context to ...
message when the piece of SQL has a USE database
in it ?
推荐答案
您需要设置errorlevel<sqlcmd
的/a>,默认为 0.注意:不要将这里的错误级别与返回到 的
作为 sqlcmd
的退出代码混淆cmd.exeERRORLEVEL
.
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
.
要为所有 sqlcmd
会话禁用此消息,请使用 -m
命令行选项:
To disable this message for all of an sqlcmd
session, use the -m
commandline option:
sqlcmd -m 1 <other options>
要为代码块禁用此消息,请使用 :setvar
批处理命令:
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
...
要在 Management Studio 中使用 :setvar
(或其他 SQLCMD 批处理命令),您需要为您所在的查询窗口启用 SQLCMD 模式(菜单查询/SQLCMD 模式").当以:"开头的行具有灰色背景时,您会看到它已启用.
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.
这篇关于如何停止“将数据库上下文更改为..."信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!