Search code examples
sqldatabaseazure-sql-databasesqlcmdsql-server-2017

How to connect to an Azure SQL Server using command prompt?


I have an Azure SQL database that I am able to connect using local SSMS.

Server Name - <Server>.database.windows.net
UserName - zrana
Password - *****

enter image description here

The authentication mode I use is here is Active Directory - Password. Is it possible to connect to the database using the sqlcmd utility on the command line?

I am unable to connect using the following command

sqlcmd -S 910005-sql.database.windows.net -d 900046 -U zrana -P ****

There is another database that I created on Azure and tried to connect through Local SSMS.

This time the authentication method is "SQL Server password". I am able to connect to it using local SSMS and through sqlcmd using the same format mentioned above.

Is it possible to connect to an Azure SQL database using AAD? I tried using -G -I options in my command and didn't work.

This is the error I see:

enter image description here


Solution

  • (Adding an answer that is essentially the process we worked through in the comments)

    1. Install the latest sqlcmd from https://learn.microsoft.com/en-us/sql/tools/sqlcmd-utility?view=sql-server-ver15
    2. run sqlcmd -? to see what version of sqlcmd is actually running - if it isn't the latest version you just downloaded, check your PATH variable etc. You can use the dos command where sqlcmd to find where the command line is searching for sqlcmd
    3. run sqlcmd with the -G switch to specify AAD cred checking, and include your user name an dpassword with the -U and -P options. Be careful with the case of these as -u and -p are different switches entirely.
    sqlcmd -S 910005-sql.database.windows.net -d 900046 -G -U zrana -P ****