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

后端 未结 11 1725
遥遥无期
遥遥无期 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:30

    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

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

    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: unlike the sp_defaultdb solution user and database names are provided without quotes. 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).

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

    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.

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

    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.

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

    In case you can't login to SQL Server:

    sqlcmd –E -S InstanceName –d master
    

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

    0 讨论(0)
提交回复
热议问题