sqlcmd
SQL Server command-line query tool
TLDR
Connect to SQL Server locally
SYNOPSIS
sqlcmd [-S server] [-U login] [-P password] [options]
DESCRIPTION
sqlcmd is Microsoft's command-line utility for SQL Server and Azure SQL. It allows executing Transact-SQL statements, stored procedures, and SQL scripts interactively or in batch mode. Two implementations exist: the newer Go-based version (go-sqlcmd) and the traditional ODBC-based version.
The utility supports various authentication methods including SQL Server authentication, Windows/Kerberos authentication, and Azure Active Directory. Results can be output to the terminal, files, or piped to other commands.
In interactive mode, commands like :quit exit the session, GO executes the statement batch, and :r includes a script file. The prompt shows the current line number.
PARAMETERS
-S server
SQL Server instance to connect to. Format: [protocol:]server[\instance][,port].-U login
User login name.-P password
Password for the login.-E
Use Windows Authentication (trusted connection).-d database
Initial database to use.-Q query
Execute query and exit.-q query
Execute query and enter interactive mode.-i file
Input file containing SQL statements.-o file
Output file for results.-N
Encryption mode: o (optional), m (mandatory, default in 2025), s (strict).-No
Encryption optional (shorthand).-C
Trust server certificate.-t timeout
Query timeout in seconds.-l timeout
Login timeout in seconds.-h headers
Rows between column headers (-1 to disable).-s separator
Column separator character.-w width
Screen width for output.-?
Display help for ODBC sqlcmd flags.--help
Display help for go-sqlcmd subcommands.
CAVEATS
Starting with SQL Server 2025, mandatory encryption (-Nm) is the default, which is a breaking change from earlier versions. Use -No for optional encryption when connecting to older servers. Password on command line is visible in process listings; consider using environment variables or prompts instead. Linux requires the mssql-tools package from Microsoft repositories.
HISTORY
sqlcmd was introduced by Microsoft as the replacement for the older osql and isql utilities. The ODBC-based version has been available since SQL Server 2005 and was ported to Linux when SQL Server on Linux was released in 2017. In 2022, Microsoft released go-sqlcmd, a Go-based reimplementation with modern features and cross-platform support. Development continues with SQL Server 2025 adding TDS 8.0 support.
