3

I work on multiple Azure databases, using AzureAD - Universal with MFA in SSMS.

I usually have a number of different scripts on the go, comparing results between the different databases, then an hr or so later, the connection is terminated by the server and if I try to re-run it, it tells me doesn't recognise the table, because it no longer knows which database to connect to.

Is there a way to specify the database within SSMS, if I try USE dbDocument before the script, it tells me Msg 40508, Level 16, State 1, Line 1 USE statement is not supported to switch between databases. Use a new connection to connect to a different database..

Is there any way around this, without having to right-click the db in question and hitting NEW QUERY?

2
  • 1
    No, I don't believe there is a way Commented Jul 24, 2020 at 9:18
  • Thanks, it was unlikely, just wondered if someone had a better way. Because of having to use AzureAD Universal with MFA authentication I'm limited to SSMS, unless you know any good, free clients. I was using Heidi, but that doesn't support it. Commented Jul 24, 2020 at 9:20

2 Answers 2

3
Answer recommended by Microsoft Azure Collective

The USE statement is not supported on Azure SQL Database. One solution is to split your work to several different steps according to the database that you want to use, and connect to the right database for each step.

If you want to execute the same query on multiple Azure Databases then you can use one of these options:

Sign up to request clarification or add additional context in comments.

Comments

2

Looking at this MS doc, I doubt this is possible at all...

If a database other than the current database is provided, the USE statement does not switch between databases, and error code 40508 is returned. To change databases, you must directly connect to the database. The USE statement is marked as not applicable to SQL Database at the top of this page, because even though you can have the USE statement in a batch, it doesn't do anything.

3 Comments

Thinking about it, that is complete rubbish. You don't have to connect again, you just have to right-click the database and hit NEW QUERY. Note that I am talking about a new DB within the same Azure Server. Either way, it's plain stupid that you can't specify the database before your query.
@aSystemOverload When you right click the database and hit "new query", it opens a new connection. That's why you get a new session_id.
@aSystemOverload, yep, the USE implementation is rubbish. But I agree with allmhuran -- clicking "new query" does issue a new connection to DB. What you were thinking, probably, was that your connection to SQL Server stays intact, which is true !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.