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

Martin Brown picture Martin Brown · Dec 12, 2008 · Viewed 249.6k times · Source

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?

Answer

Martin Brown picture Martin Brown · Dec 12, 2008

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'