Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL Server Default Database when Query - master

I've been using SQL Server Management Studio (SSMS) for the last 8 years, and I keep on stumbling upon a problem. When I right click on a Table, and select SELECT TOP 2000 ROWS, the query editor opens up a new file with the query inside. This is nice and all for a quick review of the table.

The problem I have is the default database is changed from the actual database to the master database. I have sysadmin rights.

The query that gets generated by SSMS, then has the databse, schema, and table in brackets, i.e. [DB].[dbo].[TableName]

Is there a way to set the default database on the SELECT TOP 2000 ROWS command, to NOT go and set the default database to 'master' ?

The other workaround is to click on the table, and then do a 'New Query', which will keep the current database, and then I have to type in 'SELECT * FROM TableName'

default database changed to master

like image 957
Riaan Avatar asked Jun 07 '12 08:06

Riaan


People also ask

How do I find the default database in SQL Server?

Open the database server in the Object Explorer (left panel). In Object Explorer, open Security > Logins. Right-click the user you created and choose Properties. On the General page, select a Default database.

When SQL Server is installed which databases are created by default?

SQL Server comes installed with four system databases by default. They are master, model, msdb, and TempDB.

What is default database SSMS?

Whenever you connect to any database server in SSMS, by default Master database is selected as current database. But you work on different database and every time you have to change database either by using USE statement or changing selected db by mouse. It is very irritating task.


1 Answers

In SSMS go to Security > Logins - choose your login, then right-click and choose Properties - at the bottom of the tab is an option called "Default Database" - this is what you want to change.

like image 128
Barry Kaye Avatar answered Sep 27 '22 17:09

Barry Kaye