In SQL Server 2008 R2 Management Studio, if I right-click on an object inside a specific database and choose “Select top 1000 rows ..”, the database connection for the query window always opens on ‘master’ while the table name is fully qualified as [database].[dbo].[table]. This makes it impossible to jump in and tweak out this query and insert joins, etc., to the statement without also fully-qualifying everything I add, or add a USE statement, or select the database from the drop-down menu.
Is there a setting or something that will make query windows open with a database connection of the selected object browser’s database rather than connect to ‘master’, and not fully qualify the object’s database in the query text? I realize that I can register my SQL connection to default to my database, but we actually go through multiple new databases every week–in a given month I will have touched tens of databases–so it would be difficult to manage multiple database registrations. I would rather it if SSMS just connected to the specified database. Possible and straightforward?
There is no such setting for the
SELECT TOPcommand, but you may be able to do this by changing the default database for your login. This is tedious if you’re doing this often for various databases (much like changing the registrations, as I just noticed you already outlined).Instead of using
SELECT TOP 1000(which in addition to not putting you in the right database context, also puts aTOPin that I assume you’re just going to remove as well), you should right-click the table and chooseScript Table as>SELECT to>New Query Window. This puts the context in the right DB, adds aUSEcommand, doesn’t have aTOPand doesn’t database-prefix the table name.