Establishes a connection to a Microsoft SQL Server instance through either command-line tools or graphical clients.
Command-line connections automate testing, scripting, and CI/CD workflows without manual clicks. They are predictable, easy to version-control, and faster for repetitive tasks.
sqlcmd
ships with Microsoft ODBC drivers and runs on Windows, macOS, and Linux. It lets you connect, run T-SQL scripts, and export results—all from one command.
sqlcmd
?macOS/Linux: brew install mssql-tools
or use Microsoft’s apt/yum repos. Windows: include “Client Tools Connectivity” in the SQL Server installer or install the standalone ODBC package.
sqlcmd
syntax?Use -S
for server, -d
for database, -U
for user, and -P
for password. Add -Q
to execute a query immediately or -i
to run a file.
SQL Server Management Studio (SSMS) and Azure Data Studio (ADS) offer graphical log-in dialogs. Supply the same host, database, and credentials used by sqlcmd
. Save profiles for one-click reuse.
1) Open SSMS → Connect → Database Engine. 2) Server name: mydb.company.net,1433
. 3) Authentication: SQL Login. 4) Enter user & password → Connect. A new query tab opens automatically.
sqlcmd -S mydb.company.net -d shop -U app_user -P secret -Q "SELECT TOP 10 * FROM Customers;"
Store passwords in environment variables or secret managers, then call sqlcmd -P $SQLSERVER_PWD
. For GUI tools, use OS keychains and disable “save password in plain text.”
Wrong port: SQL Server defaults to 1433; include it if the instance listens on a non-default port, e.g., -S host,1500
.
Mismatched auth mode: If Windows Auth is disabled on Linux/macOS, switch to SQL Logins or enable Kerberos tickets.
Yes, install Kerberos and use sqlcmd -E
after acquiring a ticket via kinit
. GUI clients like ADS also support Azure AD logins.
Add -N
to sqlcmd
or enable “Encrypt Connection” in SSMS. Query sys.dm_exec_connections
to verify encrypt_option = 'TRUE'
.
The login’s default database may be offline. Connect to master
first, then ALTER LOGIN to set a valid default database.