Edit

sqlcmd utility

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-tools package 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 -P switch. For SQL Server Authentication, you can provide passwords through these mechanisms:

    • The -P command-line switch
    • The SQLCMDPASSWORD environment variable
    • The :CONNECT command
    • When prompted, type the password to complete a connection
  • The -r switch requires a 0 or 1 argument.

  • The -R switch is ignored. The Go runtime doesn't provide access to user locale information.

  • The -I switch is ignored. Quoted identifiers are always enabled. To disable quoted identifier behavior, add SET QUOTED IDENTIFIER OFF in your scripts.

  • The -M switch is ignored. sqlcmd (Go) always enables multi-subnet failover.

  • The -N takes a string value to specify the encryption choice, which is one of s[trict], t[rue]/m[andatory]/yes/1, o[ptional]/no/0/f[alse], or disable.

    • If you don't provide -N and -C, sqlcmd negotiates authentication with the server without validating the server certificate.
    • If you provide -N but not -C, sqlcmd requires validation of the server certificate. A false value for encryption could still lead to the encryption of the login packet.
    • If you provide both -N and -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), -N can be o (for optional), m (for mandatory, the default), or s (for strict). If you don't include -N, -Nm (for mandatory) is the default. This behavior is a breaking change from SQL Server 2022 (16.x) and earlier versions.

  • With the -u switch, 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 OSQL might 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 by EXIT(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:

  • lpc for shared memory (localhost only)
  • np for named pipes, or use the UNC named pipe path as the server name
  • tcp for 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

  • :Connect has an optional -G parameter 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 -G isn't provided, Integrated security or SQL Server authentication is used, depending on the presence of a -U user name parameter.

  • The --driver-logging-level command line parameter allows you to see traces from the go-mssqldb driver. Use 64 to see all traces.

  • sqlcmd (Go) can print results using a vertical format. Use the --vertical command line switch to set it. The SQLCMDFORMAT scripting variable also controls it.

Command-line options

The following table lists the command-line options available in sqlcmd, and which operating systems they support.

Command-line option Supported on Windows Supported on Linux and macOS
Login-related options
-A Yes No
-C Yes Yes
-d db_name Yes Yes
-D Yes Yes
-l login_timeout Yes Yes
-E Yes Yes
-F hostname_in_certificate Yes Yes
-g Yes Yes
-G Yes Yes
-H workstation_name Yes Yes
-j Yes Yes
-J server_certificate No Yes
-K application_intent Yes Yes
-M multisubnet_failover Yes Yes
-N[s|m|o] Yes Yes
-P password Yes Yes
-S [protocol:]server[\instance_name][,port] Yes Yes
-U login_id Yes Yes
-z new_password Yes Yes
-Z new_password Yes Yes
Input/output options
-f codepage | i:codepage[,o:codepage] | o:codepage[,i:codepage] Yes Yes
-i input_file[,input_file2...] Yes Yes
-o output_file Yes Yes
-r[0 | 1] Yes Yes
-R Yes Yes
-u Yes Yes
Query execution options
-e Yes Yes
-I Yes Yes
-q "cmdline query" Yes Yes
-Q "cmdline query" Yes Yes
-t query_timeout Yes Yes
-v var = value [ var = value... ] Yes No
-x Yes Yes
Format options
-h headers Yes Yes
-k [1 | 2] Yes Yes
-s col_separator Yes Yes
-w screen_width Yes Yes
-W Yes Yes
-y variable_length_type_display_width Yes Yes
-Y fixed_length_type_display_width Yes Yes
Error reporting options