ChatGPT解决这个技术问题 Extra ChatGPT

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

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?


M
Martin Brown

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' 

Thanks indeed. Worked like a charm (and taught me about the 'Options' dialogue to specify db).
This question has raised a question on meta
Doesn't work so well if you're in an AD group and you don't want to change the default database for dozens of other developers.
@DOK: But I would assume that if your log in is broken in this way so are all the other developers. So fixing it for all of them would surely be a good thing?
@MartinBrown Dozens of developers, dozens of databases, developers (contractors) coming and going all the time, shifting among projects. Someone would have to administer all those logins and roles. For security, you want to limit access to those who need it. So, a developer could wait around for the admin to grant permission on another database. Meanwhile, when the auditors come around, they ding you for having stale permissions for folks who no longer need them. Sometimes it's better to use the simplest approach: add or remove the person to AD groups. There is no one-size-fits-all approach.
M
Marek

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]

Well this question is getting a bit old now. At the time I was using SQL 2005. Alter Login wasn't added until the 2008 version.
@MartinBrown Yes, I've been using sp_deafultdb for ages. I was surprised that there is a "new" way.
Note: single quotes not needed around fields my_user_name or database and may cause an error. Obvious to experienced hands but maybe not so to those doing this occasionally. Square brackets optional, if fields represent contiguous text.
I connect to the PROD database via a group, so I don't have an individual Login on the server, when I try to run the above I get "Cannot alter the login 'DOMAIN\myuser', because it does not exist or you do not have permission.". even worse I'm in multiple groups because each one gives me specific access to one of the many DBs on the server. suggestions?
To find what a login's default database is: select name, default_database_name from sys.server_principals;
M
Marcus Erickson

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.


this works fine! just typed there "master" db instead "default".
The issue I was having was that I was the sysadmin and had deleted the database I had set to default and as such couldn't even get to this dialog.
@MartinBrown be that as it may, I hadn't dropped any tables and I was still led to your question, but felt more comfortable with the GUI solution.
d
drac_o

Thanks to this post, I found an easier answer:

Open Sql Server Management Studio Go to object Explorer -> Security -> Logins Right click on the login and select properties And in the properties window change the default database and click OK.


Welcome to StackOverflow and thanks for providing an answer. You might want to take a look at the features of the Markdown editor, so you can format code blocks, lists, etc.: stackoverflow.com/editing-help. I've edited your answer to improve the formatting for you.
The whole point of this question is that what you have described does not work if you have deleted the default database for your own login account. In my particular case I was the only administrator in the system so could not use someone else's account to achieve the same effect either.
@Mattijs Hope you read this last line of the question "Does anyone know how to set my default database without using object explorer?". Thats is just what you have asked to do.
This mightn't have answered the OP's question, but it answered mine. It's a useful answer.
This is not possible at Azure Databases :/
m
marc_s

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


R
Ravishankar S

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


For some reason this has worked for me in some instances and not in others.
C
Chand

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.

https://i.stack.imgur.com/v9EzQ.png

https://i.stack.imgur.com/6FaE3.png


From the question: " 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." Though they may have fixed it since then, this question is quite old.
M
Mubashar

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

But for GUI lover

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

Tired of reading!!! just look at following

https://i.stack.imgur.com/A1gju.png


g
gmesorio

In case you can't login to SQL Server:

sqlcmd –E -S InstanceName –d master

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


v
vapcguy

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

A
Adam Diament

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