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

后端 未结 11 1724
遥遥无期
遥遥无期 2020-11-30 17:48

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 Man

相关标签:
11条回答
  • 2020-11-30 18:21

    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' 
    
    0 讨论(0)
  • 2020-11-30 18:22

    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

    0 讨论(0)
  • 2020-11-30 18:24

    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
    
    0 讨论(0)
  • 2020-11-30 18:26

    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

    0 讨论(0)
  • 2020-11-30 18:27

    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.

    0 讨论(0)
  • 2020-11-30 18:28
    1. Click on Change Connection icon
    2. Click Options<<
    3. Select the db from Connect to database drop down
    0 讨论(0)
提交回复
热议问题