0

I've been searching for this answer and haven't had any luck. My problem is that whenever I
Select top 1000 from tblX it defaults the database to Master and I have to manually change to the correct DB. I know on my VDM at work when doing this, it adds a Use command which specifies the correct database and gives me a connection instantly. I've read that you can change the default database, but I will be switching back and forth between many databases. So I want my connection defaulted to which ever database the selected table is from.

CSharper
  • 5,420
  • 6
  • 28
  • 54
  • 1
    Are you using SQL Server Management Studio? The default behavior is to schema qualify the table in the code generated statements `database.dbo.table`. Also check [this question](http://stackoverflow.com/questions/3806245/sql-server-schema-and-default-schema). – KingOfAllTrades Sep 05 '14 at 02:54
  • 1
    If I got it correctly you mean that you want to auto-select the database of the table which you are generating code for. I don't think there is a native way, but maybe you can workaround using the "Set As Active Connection" of the [SSMSBoost add-on](http://www.ssmsboost.com). Another way could be to edit or create a SQL Server template from the Template Explorer in your SQL Server Management Studio. You can simply add the USE command with a template (however you need to add the parameter value manually). – Alessandro Alpi Sep 05 '14 at 23:29

2 Answers2

2

You can use [db name].dbo.[table name]

radar
  • 13,270
  • 2
  • 25
  • 33
  • Yes, but after selecting from a database and a new query window opens it defaults to master, I want it to default to whatever db the selected table is from. This gets done by the Use command being generated while selecting top * 1000 from table – CSharper Sep 05 '14 at 02:32
  • Looks like there is already another post with same question which suggests some setting changes http://stackoverflow.com/questions/7072575/sql-server-management-studio-connection-defaults-to-master-when-selecting-a-da – radar Sep 05 '14 at 02:37
  • i saw that question too it wasnt what i was looking for – CSharper Sep 05 '14 at 03:03
0

You could always change the default schema for you user. But this is not my preffered approach. You should always use two part name naming or in your case 3 part according to the 70-461 Training kit.

Changing Default Schema

Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188