23

Is there some way to stop the Changed database context to ... message when the piece of SQL has a USE database in it ?

2
  • 1
    Where do you get these messages? I've never seen one before. May 2, 2012 at 11:21
  • 1
    @ta.speot.is you see them, for example, when using SQLCMD.exe? May 2, 2012 at 11:29

5 Answers 5

40

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.

4
  • Hi, I got this message when running an application built using delphi. I need to modify the databases in between.Your way is working in SQL management studio but when i tried to pass the cmd from code i got error as "Incorrect syntax near '@P1'.Does anyone have a solution for this?
    – kvsbhavani
    Dec 30, 2015 at 10:17
  • 1
    @kvsbhavani That is expected. The :setvar syntax is part of the so called "batch mode", which is only supported by SQLCMD.EXE and SSMS (see the last paragraph of my answer). If you are handling this in your own code I suggest you post a new question on SO. Dec 30, 2015 at 10:20
  • I saw in 14 version of SqlCmd that SQLCMDERRORLEVEL = 1 hides only "Changed database context to ..." and doesn't hide the PRINT output. But you can get troubles if anybody users older versions of SqlCmd.
    – it3xl
    Sep 19, 2018 at 20:44
  • In this case GO seems to be required. I usually end my use statements with just a semi-colon, but including GO was the only I could get this to work. Actually, adding the :setvar statements without the GO just made it so not even adding -m 1 to sqlcmd call would suppress the "context changed" messages. Oct 19, 2022 at 22:29
7

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

1
  • This is a great idea as it means I can run the script in the query window (without having to turn SQLCMD mode on) and use it in with SQLCMD without it obscuring error messages because of the repeated context change output.
    – LawrenceF
    Sep 2, 2016 at 6:27
6

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;

2
  • Thanks for the pointer ... not sure I fancy disambiguating all my code :-)
    – SteveC
    May 3, 2012 at 9:45
  • possibly it didn't work for me because I still had the mssql_select_db call after connecting (which afaik also issues a USE command)... I don't know - I solved the problem by adding a SELECT 1 after every USE (like user2335044 suggested)
    – Algoman
    Apr 30, 2014 at 6:38
2

In my case, an easy and simple solution was to run a small query first, such as SELECT 1;. The message Changed database context... was therefore coupled to this first query and the following queries were retrieved without this error message.

1
  • 1
    I got this message while executing a PHP query that previously was working. To fix the issue I've incremented the mssql.timeout value in php.ini from 60 to 120. The problem arose because a query was taking more than 60 seconds and the DB/Driver? (freetds) was giving "Changed database context to" message instead of a more meaningful and related error to a timeout problem. Sep 5, 2018 at 8:50
0

Simple solution: add -m 1 to your sqlcmd command line arguments.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.