41

I would like to change the default database for a login to support software that can access SQL Azure but does not allow easy alteration of a connection string. It appears that SQL Azure defaults to the master database.

I've already considered:

  • Stored procedures. I can't find a stored procedure that does this (sp_defaultdb is not implemented in SQL Azure as far as I can tell)

  • Alter Login. ALTER LOGIN does not permit the DEFAULT_DATABASE option.

  • SSMS. SSMS doesn't seem to allow much user control through the interface for SQL Azure.

Ideas?

1
  • 1
    Sadly your question covers all the bases - I don't think it's possible. As the answers below suggest, you're kind of stuck changing which database you connect to through your connection string or the application you are connecting with (in my case SSMS). Apr 17, 2019 at 7:02

7 Answers 7

11

Connection String:

Server=tcp:[serverName].database.windows.net;Database=myDataBase;User ID=[LoginForDb]@[serverName];Password=myPassword;Trusted_Connection=False;Encrypt=True;

You can change connect default database, when you write 'Database' property at connection string.

SSMS is connect to database box which is option->secont tab at conecct dialog.

9

In my case I managed to do it by creating a login directly in the master database:

CREATE LOGIN newlogin WITH password='password'
GO

CREATE USER newlogin FOR LOGIN newlogin WITH DEFAULT_SCHEMA=[dbo]
GO

Then I created a user in the database I wanted to gain access to:

CREATE USER newlogin FOR LOGIN newlogin WITH DEFAULT_SCHEMA=[dbo]
GO

At last adding it to a role in the particular database to gain the right permissions:

Alter role db_owner add member newlogin

Hope this works for you too.

0
8

As you already discovered the DEFAULT_DATABASE option is not available in SQL Azure. So if you cannot change the connection string of your application in which you would normally specify the database name, you are stuck to master.

However... is it possible to create an ODBC connection, and configure your application to use ODBC? Using ODBC would allow you to specify a default database.

2
  • Thanks... sounds like I am mostly stuck unless I want to construct a connetion string. I was trying to avoid that, but it sounds like there really is no other way.
    – Lorin H
    Jan 18, 2012 at 3:43
  • See @Rob Sedgwick's solution above as to how this is possible.
    – JoeJam
    Jul 5, 2022 at 14:34
6

By far the easiest way in SSMS is to use the additional parameters tab and supply the initial catalog, e.g.

Initial Parameters tab

1
  • 2
    for the same effect can be used tab "Connection Properties" and field "Connect to database" (it does exist there in the 2020 year)
    – KEMBL
    Sep 4, 2020 at 15:24
3

You can easily change the default database when loging in with SSMS. Click the Options button on the Login Dialog then click on the Connection Properties tab. In the "Connect to database" box enter the name of your database.

2
  • 8
    I don't think that's changing the default database. It's just allowing you to connect to a different database when you initially log in from SSMS
    – Andy
    Aug 24, 2018 at 10:57
  • Agreed, this does not actually change the default, and is not a solution.
    – hmiedema9
    Aug 29, 2019 at 15:08
2

You have to use use following to be able to change "default" Database

  1. Use "SQL Server Native Client 10.0" or higher instead on using "SQL Server as Driver
  2. Use full user id like UserName@AzureConnnectString

For me it is NorthWind@w6ywertsd8h.database.windows.net

More details here http://debugmode.net/2011/04/22/connecting-microsoft-access-to-sql-azure/

2
  • Is "NorthWind" in your example a user or database name? Jul 5, 2017 at 10:56
  • This does not seem to be changing the default database in any way. Yes, the "SQL Server Native Client 10.0" will allow you to connect to any DB, but you will still have to specify it somehow (e.g. in the resulting connection string). The use of full user ID does not impact the database chosen. The part you name "AzureConnectionString" in point 2 should not be a connection string, but the Azure SQL Server name, and in your case "NorthWind@w6ywertsd8h" should be enough.
    – Hilarion
    Dec 5, 2018 at 19:18
0

Another option is to create a mapped user in the master database and the hosted database. This will allow SSMS to connect to the server and use master as the default db, then the user can open the database. I am not a DBA so I do not know the implications of this, but that is how I solved it. My database is just being used for a POC project so it doesn't have many security requirements.

1
  • You're a lifesaver.
    – Ryan
    Mar 9, 2020 at 18:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.