How can I change my default database in SQL Server without using MS SQL Server Management Studio?

Sql ServerSql Server-2005SsmsDefault Database

Sql Server Problem Overview


I dropped a database from SQL Server, however it turns out that my login was set to use the dropped database as its default. I can connect to SQL Server Management Studio by using the 'options' button in the connection dialog and selecting 'master' as the database to connect to. However, whenever I try to do anything in object explorer, it tries to connect using my default database and fails.

Does anyone know how to set my default database without using object explorer?

Sql Server Solutions


Solution 1 - Sql Server

What you can do is set your default database using the sp_defaultdb system stored procedure. Log in as you have done and then click the New Query button. After that simply run the sp_defaultdb command as follows:

Exec sp_defaultdb @loginame='login', @defdb='master' 

Solution 2 - Sql Server

Alternative to sp_defaultdb (which will be removed in a future version of Microsoft SQL Server) could be ALTER LOGIN:

ALTER LOGIN [my_user_name] WITH DEFAULT_DATABASE = [new_default_database]

Note: user and database names are provided without quotes (unlike the sp_defaultdb solution). Brackets are needed if name had special chars (most common example will be domain user which is domain\username and won't work without brackets):

ALTER LOGIN me WITH DEFAULT_DATABASE = my_database

but

ALTER LOGIN [EVILCORP\j.smith28] WITH DEFAULT_DATABASE = [prod\v-45]

Solution 3 - Sql Server

To do it the GUI way, you need to go edit your login. One of its properties is the default database used for that login. You can find the list of logins under the Logins node under the Security node. Then select your login and right-click and pick Properties. Change the default database and your life will be better!

Note that someone with sysadmin privs needs to be able to login to do this or to run the query from the previous post.

Solution 4 - Sql Server

Thanks to this post, I found an easier answer:

  1. Open Sql Server Management Studio

  2. Go to object Explorer -> Security -> Logins

  3. Right click on the login and select properties

  4. And in the properties window change the default database and click OK.

Solution 5 - Sql Server

If you don't have permissions to change your default DB you could manually select a different DB at the top of your queries...

USE [SomeOtherDb]
SELECT 'I am now using a different DB'

Will work as long as you have permission to the other DB

Solution 6 - Sql Server

  1. Click on Change Connection icon
  2. Click Options<<
  3. Select the db from Connect to database drop down

Solution 7 - Sql Server

Click on options on the connect to Server dialog and on the Connection Properties, you can choose the database to connect to on startup. Its better to leave it default which will make master as default. Otherwise you might inadvertently run sql on a wrong database after connecting to a database.

enter image description here

enter image description here

Solution 8 - Sql Server

I'll also prefer ALTER LOGIN Command as in accepted answer and described here

But for GUI lover

  1. Go to [SERVER INSTANCE] --> Security --> Logins --> [YOUR LOGIN]
  2. Right Click on [YOUR LOGIN]
  3. Update the Default Database Option at the bottom of the page

Tired of reading!!! just look at following

enter image description here

Solution 9 - Sql Server

In case you can't login to SQL Server:

sqlcmd –E -S InstanceName –d master

Reference: https://support.microsoft.com/en-us/kb/307864

Solution 10 - Sql Server

This may or may not exactly answer the question, but I ran into this issue (and question) when I had changed my account to have a new database I had created as my "default database". Then I deleted that database and wanted to test my creation script, from scratch. I logged off SSMS and was going to go back in, but was denied -- cannot log into default database was the error. D'oh!

What I did was, on the login dialog for SSMS, go to Options, Connection Properties, then type master on the "Connect to database" combobox. Click Connect. Got me in. From there you can run the command to:

ALTER LOGIN [DOMAIN\useracct] WITH DEFAULT_DATABASE=[master]
GO

Solution 11 - Sql Server

If you use windows authentication, and you don't know a password to login as a user via username and password, you can do this: on the login-screen on SSMS click options at the bottom right, then go to the connection properties tab. Then you can type in manually the name of another database you have access to, over where it says , which will let you connect. Then follow the other advice for changing your default database

https://gyazo.com/c3d04c600311c08cb685bb668b569a67

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMartin BrownView Question on Stackoverflow
Solution 1 - Sql ServerMartin BrownView Answer on Stackoverflow
Solution 2 - Sql ServerMarekView Answer on Stackoverflow
Solution 3 - Sql ServerMarcus EricksonView Answer on Stackoverflow
Solution 4 - Sql ServerMattijsView Answer on Stackoverflow
Solution 5 - Sql ServerGreg BView Answer on Stackoverflow
Solution 6 - Sql ServerRavishankar SView Answer on Stackoverflow
Solution 7 - Sql ServerChandView Answer on Stackoverflow
Solution 8 - Sql ServerMubasharView Answer on Stackoverflow
Solution 9 - Sql ServergmesorioView Answer on Stackoverflow
Solution 10 - Sql ServervapcguyView Answer on Stackoverflow
Solution 11 - Sql ServerAdam DiamentView Answer on Stackoverflow