How can I change my default database in SQL Server

2019-01-08 04:12发布

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?

9条回答
smile是对你的礼貌
2楼-- · 2019-01-08 04:25

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

查看更多
forever°为你锁心
3楼-- · 2019-01-08 04:25

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

查看更多
Deceive 欺骗
4楼-- · 2019-01-08 04:27
  1. Click on Change Connection icon
  2. Click Options<<
  3. Select the db from Connect to database drop down
查看更多
我欲成王,谁敢阻挡
5楼-- · 2019-01-08 04:29

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' 
查看更多
Juvenile、少年°
6楼-- · 2019-01-08 04:29

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.

查看更多
ゆ 、 Hurt°
7楼-- · 2019-01-08 04:31

In case you can't login to SQL Server:

sqlcmd –E -S InstanceName –d master

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

查看更多
登录 后发表回答