Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
SQL Server
Azure SQL Database
Azure SQL Managed Instance
Azure Synapse Analytics
Analytics Platform System (PDW)
SQL database in Microsoft Fabric
Use the sqlcmd utility to enter Transact-SQL statements, system procedures, and script files through various modes:
- At the command prompt.
- In Query Editor in SQLCMD mode.
- In a Windows script file.
- In an operating system (
cmd.exe) job step of a SQL Server Agent job.
Note
While Microsoft Entra ID is the new name for Azure Active Directory (Azure AD), to prevent disrupting existing environments, Azure AD still remains in some hardcoded elements such as UI fields, connection providers, error codes, and cmdlets. In this article, the two names are interchangeable.
sqlcmd variants
Two variants of sqlcmd exist:
sqlcmd (Go): The
go-mssqldb-based sqlcmd, sometimes styled as go-sqlcmd. This version is a standalone tool you can download independently of SQL Server. It runs on Windows, macOS, Linux, and in containers.sqlcmd (ODBC): The platform-aligned, ODBC-based sqlcmd, available with SQL Server or the Microsoft Command Line Utilities, and part of the
mssql-toolspackage on Linux. It also runs on Windows, macOS, Linux, and in containers.
To find out which variant and version of sqlcmd is installed on your system, see Check installed version of sqlcmd utility.
For information on how to get sqlcmd, see Download and install the sqlcmd utility.
TDS 8.0 support
SQL Server 2025 (17.x) introduces TDS 8.0 support for the sqlcmd utility.
Syntax
In this article, the terms option, parameter, command-line argument, and switch are interchangeable.
sqlcmd (Go) has two help modes: --help for modern subcommands and -? for ODBC-compatible flags.
Modern commands (--help)
Usage:
sqlcmd [flags]
sqlcmd [command]
Examples:
# Install/Create, Query, Uninstall SQL Server
sqlcmd create mssql --accept-eula --using https://aka.ms/AdventureWorksLT.bak
sqlcmd open ads
sqlcmd query "SELECT @@version"
sqlcmd delete
# View configuration information and connection strings
sqlcmd config view
sqlcmd config cs
Available Commands:
completion Generate the autocompletion script for the specified shell
config Modify sqlconfig files using subcommands like "sqlcmd config use-context mssql"
create Install/Create SQL Server, Azure SQL, and Tools
delete Uninstall/Delete the current context
help Help about any command
open Open tools (e.g ADS) for current context
query Run a query against the current context
start Start current context
stop Stop current context
Flags:
-?, --? help for backwards compatibility flags (-S, -U, -E etc.)
-h, --help help for sqlcmd
--sqlconfig string configuration file (default "/Users/<currentUser>/.sqlcmd/sqlconfig")
--verbosity int log level, error=0, warn=1, info=2, debug=3, trace=4 (default 2)
--version print version of sqlcmd
Use "sqlcmd [command] --help" for more information about a command.
ODBC-compatible flags (-?)
sqlcmd
-a packet_size
-A (dedicated administrator connection)
-b (terminate batch job if there is an error)
-c batch_terminator
-C (trust the server certificate)
-d db_name
-e (echo input)
-E (use trusted connection)
-F hostname_in_certificate
-g (enable column encryption)
-G (use Azure Active Directory for authentication)
-h rows_per_header
-H workstation_name
-i input_file
-I (enable quoted identifiers, always on)
-k[1 | 2] (remove or replace control characters)
-K application_intent
-l login_timeout
-L[c] (list servers, optional clean output)
-m error_level
-M multisubnet_failover (always enabled)
-N[s|m|o] (encrypt connection)
-o output_file
-P password
-q "cmdline query"
-Q "cmdline query" (and exit)
-r[0 | 1] (msgs to stderr)
-R (ignored, client regional settings not used)
-s col_separator
-S [protocol:]server[instance_name][,port]
-t query_timeout
-u (unicode output file)
-U login_id
-v var = "value"
-V error_severity_level
-w screen_width
-W (remove trailing spaces)
-x (disable variable substitution)
-X[1] (disable commands, startup script, environment variables, optional exit)
-y variable_length_type_display_width
-Y fixed_length_type_display_width
-z new_password
-Z new_password (and exit)
--authentication-method (Azure SQL authentication method)
--driver-logging-level (mssql driver log level)
--vertical (print results in vertical format)
-? (usage)
Breaking changes from sqlcmd (ODBC)
Several switches and behaviors are different in the sqlcmd (Go) utility. For the most up-to-date list of missing flags for backward compatibility, see the Prioritize implementation of back-compat flags GitHub discussion.
sqlcmd (Go) supports the
-Pswitch. For SQL Server Authentication, you can provide passwords through these mechanisms:- The
-Pcommand-line switch - The
SQLCMDPASSWORDenvironment variable - The
:CONNECTcommand - When prompted, type the password to complete a connection
- The
The
-rswitch requires a0or1argument.The
-Rswitch is ignored. The Go runtime doesn't provide access to user locale information.The
-Iswitch is ignored. Quoted identifiers are always enabled. To disable quoted identifier behavior, addSET QUOTED IDENTIFIER OFFin your scripts.The
-Mswitch is ignored. sqlcmd (Go) always enables multi-subnet failover.The
-Ntakes a string value to specify the encryption choice, which is one ofs[trict],t[rue]/m[andatory]/yes/1,o[ptional]/no/0/f[alse], ordisable.- If you don't provide
-Nand-C, sqlcmd negotiates authentication with the server without validating the server certificate. - If you provide
-Nbut not-C, sqlcmd requires validation of the server certificate. Afalsevalue for encryption could still lead to the encryption of the login packet. - If you provide both
-Nand-C, sqlcmd uses their values for encryption negotiation. - For more information about client/server encryption negotiation, see MS-TDS PRELOGIN.
Important
In SQL Server 2025 (17.x),
-Ncan beo(foroptional),m(formandatory, the default), ors(forstrict). If you don't include-N,-Nm(formandatory) is the default. This behavior is a breaking change from SQL Server 2022 (16.x) and earlier versions.- If you don't provide
With the
-uswitch, the generated Unicode output file is prefixed with the UTF-16 little-endian byte-order mark (BOM).Some behaviors that were kept to maintain compatibility with
OSQLmight have changed, such as alignment of column headers for some data types.All commands must fit on one line, even
EXIT. Interactive mode doesn't check for open parentheses or quotes for commands, and doesn't prompt for successive lines. This behavior is different from the ODBC version, which allows the query run byEXIT(query)to span multiple lines.
sqlcmd (Go) supports shared memory, named pipes, and TCP transport. Use the appropriate protocol prefix on the server name to force a protocol:
lpcfor shared memory (localhost only)npfor named pipes, or use the UNC named pipe path as the server nametcpfor TCP
If you don't specify a protocol, sqlcmd tries to dial in this order: lpc > np > tcp. When connecting to a remote host, lpc is skipped.
Enhancements
:Connecthas an optional-Gparameter to select one of the authentication methods for Azure SQL Database -SqlAuthentication,ActiveDirectoryDefault,ActiveDirectoryIntegrated,ActiveDirectoryServicePrincipal,ActiveDirectoryManagedIdentity,ActiveDirectoryPassword. For more information, see Authenticate with Microsoft Entra ID in sqlcmd. If-Gisn't provided, Integrated security or SQL Server authentication is used, depending on the presence of a-Uuser name parameter.The
--driver-logging-levelcommand line parameter allows you to see traces from thego-mssqldbdriver. Use64to see all traces.sqlcmd (Go) can print results using a vertical format. Use the
--verticalcommand line switch to set it. TheSQLCMDFORMATscripting variable also controls it.
Command-line options
The following table lists the command-line options available in sqlcmd, and which operating systems they support.