HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to open queries into a selected database in SQL Server Management Studio 2005

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
studiosqlintoopendatabasemanagement2005selectedhowserver

Problem

My situation:

Using Microsoft SQL Server Management Studio 2005, I connect to a server -- I’ll call it Prod -- using Windows Authentication.

Within this Prod server there are multiple databases: dbA, dbB and dbC.

On a previous computer, if I had the ‘Tables’ folder for a particular database (e.g. dbB) selected and opened an existing query (.sql file) from a local or network directory, the existing query would open “connected” to the selected database (dbB). On my replacement computer, existing queries always open connected to 'master' on the SQL Editor toolbar instead of the expected dbB database.

A minor inconvenience, yes, but if there is a simple setting I can modify or update to restore this preferred behavior (queries open into previously selected database), I'd appreciate learning how to do it.

Thanks in advance for any suggestions.

Solution

Some options that you have:

  • pointed already by gbn (change current connection)



  • click first in the databases tree and then click 'new query' or 'open file' - they will be open in the scope of the previously selected database



  • choose a preferred (default) database for the user (either in Security - user properties or in connection preferences)



  • always use USE in your queries and not rely on Management Studio :-)



  • add to the previous option the sqlcmd mode of Management Studio (tools - options - query execution - use sqlcmd mode) and this allows you to make use in your scripts of the :connect command which will allow you to run a specific script only in the context of a specific server

Context

StackExchange Database Administrators Q#3719, answer score: 8

Revisions (0)

No revisions yet.