Is there some way to stop the Changed database context to ...
message when the piece of SQL has a USE database
in it ?
相关问题
- sql execution latency when assign to a variable
- What is the best way to cache a table from a (SQL)
- php PDO::FETCH_ASSOC doesnt detect select after ba
- Bulk update SQL Server C#
- SQL to Parse a Key-Value String
相关文章
- Entity Framework 4.3.1 failing to create (/open) a
- Code for inserting data into SQL Server database u
- SQL Server 2008 Change Data Capture, who made the
- Delete Every Alternate Row in SQL
- Linux based PHP install connecting to MsSQL Server
- SQL Azure Reset autoincrement
- How do we alias a Sql Server instance name used in
- Is recursion good in SQL Server?
You need to set the errorlevel of
sqlcmd
, which defaults to 0. Note: don't confuse the errorlevel here with the exit code ofsqlcmd
that is returned to, say,cmd.exe
as theERRORLEVEL
.To disable this message for all of an
sqlcmd
session, use the-m
commandline option:To disable this message for a block of code, use the
:setvar
batch command: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.In my case, an easy and simple solution was to run a small query first, such as
SELECT 1;
. The messageChanged database context...
was therefore coupled to this first query and the following queries were retrieved without this error message.I release updates by having SQLCMD run all my .sql scripts in a directory. But when you start everything with a USE myDB you get a repetitive changed context message in the log file, which is dull. So I use this one liner instead. If the context is actually changed, you still get the message, which is good.
IF EXISTS(SELECT DB_NAME() WHERE DB_NAME() not IN ('myDB')) USE MyDB
Another idea is to use three-parts names in your SQL, e.g. instead of...
USE Pubs; SELECT name FROM dbo.Authors;
...write...
SELECT name FROM Pubs.dbo.Authors;