Uređivanje

Dijeli putem


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
-b Yes Yes
-m error_level Yes Yes
-V error_severity_level Yes Yes
Miscellaneous options
-a packet_size Yes Yes
-c batch_terminator Yes Yes
-L[c] Yes No
-p[1] Yes Yes
-X[1] Yes Yes
-? Yes Yes

-A

Applies to: Windows only. Linux and macOS aren't supported.

Signs in to SQL Server with a dedicated administrator connection (DAC). Use this kind of connection to troubleshoot a server. This connection works only with server computers that support DAC. If DAC isn't available, sqlcmd generates an error message, and then exits. For more information about DAC, see Diagnostic connection for database administrators. The -A option isn't supported with the -G option. When connecting to Azure SQL Database by using -A, you must be an administrator on the logical SQL server. DAC isn't available for a Microsoft Entra administrator.

Note

For information on how to make a dedicated administrator connection (DAC) on macOS or Linux, see Programming Guidelines.

-C

Use this option to configure the client to implicitly trust the server certificate without validation. This option is equivalent to the ADO.NET option TRUSTSERVERCERTIFICATE = true.

For the sqlcmd (Go) utility, the following conditions also apply:

  • 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.

-d db_name

Issues a USE <db_name> statement when you start sqlcmd. This option sets the sqlcmd scripting variable SQLCMDDBNAME. This parameter specifies the initial database. The default is your login's default-database property. If the database doesn't exist, an error message is generated and sqlcmd exits.

-D

Interprets the server name provided to -S as a DSN instead of a hostname. For more information, see DSN support in sqlcmd and bcp.

Note

The -D option is only available on Linux and macOS clients. On Windows clients, it refers to an obsolete option which is removed, and is ignored.

-l login_timeout

Specifies the number of seconds before a sqlcmd login to the ODBC driver times out when you try to connect to a server. This option sets the sqlcmd scripting variable SQLCMDLOGINTIMEOUT. The default time-out for login to sqlcmd is 8 seconds. When using the -G option to connect to Azure SQL Database or Azure Synapse Analytics and authenticate with Microsoft Entra ID, a timeout value of at least 30 seconds is recommended. The login timeout must be a number between 0 and 65534. If the value isn't numeric, or doesn't fall into that range, sqlcmd generates an error message. A value of 0 specifies time-out to be infinite.

-E

Uses a trusted connection instead of using a user name and password to sign in to SQL Server. By default, without -E specified, sqlcmd uses the trusted connection option.

The -E option ignores possible user name and password environment variable settings such as SQLCMDPASSWORD. If the -E option is used together with the -U option or the -P option, an error message is generated.

Note

For more information about making trusted connections that use integrated authentication from a Linux or macOS client, see Using Integrated Authentication.

-F hostname_in_certificate

Specifies a different, expected Common Name (CN) or Subject Alternate Name (SAN) in the server certificate to use during server certificate validation. Without this option, certificate validation ensures that the CN or SAN in the certificate matches the server name to which you're connecting. You can use this parameter when the server name doesn't match the CN or SAN, for example, when using DNS aliases.

For example:

sqlcmd -S server01 -Q "SELECT TOP 100 * FROM WideWorldImporters.Sales.Orders" -A -Ns -F server01.adventure-works.com

Note

sqlcmd (Go) also uses -F to specify the host name in the server certificate. To print results in vertical format, sqlcmd (Go) uses the --vertical switch instead.

-g

Sets the Column Encryption setting to Enabled. For more information, see Always Encrypted. Only master keys stored in Windows Certificate Store are supported. The -g option requires at least sqlcmd version 13.1. To determine your version, run sqlcmd -?.

-G

Use this option to authenticate with Microsoft Entra when connecting to Azure SQL Database or Azure Synapse Analytics. This option sets the sqlcmd scripting variable SQLCMDUSEAAD = true. The -G option requires at least sqlcmd version 13.1. To determine your version, run sqlcmd -?. For more information, see Microsoft Entra authentication for Azure SQL. The -A option isn't supported with the -G option.

The -G option only applies to Azure SQL Database and Azure Synapse Analytics.

Microsoft Entra interactive authentication isn't currently supported on Linux or macOS. Microsoft Entra integrated authentication requires Download ODBC Driver for SQL Server version 17.6.1 or higher and a properly configured Kerberos environment.

For more information about Microsoft Entra authentication, see Authenticate with Microsoft Entra ID in sqlcmd.

-H workstation_name

A workstation name. This option sets the sqlcmd scripting variable SQLCMDWORKSTATION. The workstation name appears in the hostname column of the sys.sysprocesses catalog view, and can be returned by using the sp_who stored procedure. If you don't specify this option, the default is the current computer name. Use this name to identify different sqlcmd sessions.

-j

Prints raw error messages to the screen.

-J server_certificate

Applies to: sqlcmd (ODBC), Linux and macOS only. Windows isn't supported.

Specifies the path to a server certificate file. This file is matched against the server's connection encryption certificate. The match is done instead of standard certificate validation (expiry, host name, trust chain, and so on). The accepted certificate formats are PEM, DER, and CER.

Use this option when connecting to servers that use self-signed certificates or certificates issued by a private certificate authority. If encryption is enabled and certificate validation fails, the connection fails.

For example:

sqlcmd -S server01 -Q "SELECT TOP 100 * FROM WideWorldImporters.Sales.Orders" -A -Ns -J /etc/ssl/certs/server_certificate.cer

-K application_intent

Declares the application workload type when connecting to a server. The only currently supported value is ReadOnly. If you don't specify -K, sqlcmd doesn't support connectivity to a secondary replica in an availability group. For more information, see Offload read-only workload to secondary replica of an Always On availability group.

Note

-K isn't supported in SUSE Linux Enterprise Server (SLES). You can, however, specify the ApplicationIntent=ReadOnly keyword in a DSN file passed to sqlcmd. For more information, see DSN Support in sqlcmd and bcp later in this article.

For more information, see High availability and disaster recovery on Linux and macOS.

-M multisubnet_failover

Always specify -M when connecting to the availability group listener of a SQL Server availability group or a SQL Server Failover Cluster Instance. -M provides for faster detection of and connection to the (currently) active server. If you don't specify -M, -M is off.

For more information, see:

Note

-M isn't supported in SUSE Linux Enterprise Server (SLES). You can, however, specify the MultiSubnetFailover=Yes keyword in a DSN file passed to sqlcmd. For more information, see DSN Support in sqlcmd and bcp later in this article.

For more information, see High availability and disaster recovery on Linux and macOS.

-N[s|m|o]

The client uses this option to request an encrypted connection.

The -N switch can be o (for optional), m (for mandatory, the default), or s (for strict). If you don't include -N, the default is -Nm (for mandatory). This default is a breaking change from SQL Server 2022 (16.x) and earlier versions, where the default is -No.

For the sqlcmd (Go) utility, -N takes a string value that can be one of true, false, or disable to specify the encryption choice. (default is the same as omitting the parameter):

  • 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.

-P password

A user-specified password. Passwords are case-sensitive. If you use the -U option, but don't use the -P option or set the SQLCMDPASSWORD environment variable, sqlcmd prompts the user for a password. Don't use a null (blank) password, but you can specify the null password by using a pair of contiguous double-quotation marks for the parameter value ("").

Important

Using -P is insecure. Avoid giving the password on the command line. Alternatively, use the SQLCMDPASSWORD environment variable, or interactively input the password by omitting the -P option.

Use a strong password.

The password prompt is displayed by printing the password prompt to the console, as follows: Password:

User input is hidden. This means that nothing is displayed and the cursor stays in position.

The SQLCMDPASSWORD environment variable lets you set a default password for the current session. Therefore, passwords don't have to be hard-coded into batch files. The following example first sets the SQLCMDPASSWORD variable at the command prompt and then accesses the sqlcmd utility.

At the command prompt, type the following command. Replace <password> with a valid password.

SET SQLCMDPASSWORD=<password>
sqlcmd
SET SQLCMDPASSWORD=<password>
sqlcmd
SET SQLCMDPASSWORD=<password>
sqlcmd

If the user name and password combination is incorrect, an error message is generated.

Note

The OSQLPASSWORD environment variable is kept for backward compatibility. The SQLCMDPASSWORD environment variable takes precedence over the OSQLPASSWORD environment variable. This means that sqlcmd and osql can be used next to each other without interference. Old scripts continue to work.

If you use the -P option with the -E option, an error message is generated.

If you use the -P option with more than one argument, an error message is generated and the program exits.

A password containing special characters can generate an error message. You should escape special characters when using -P, or use the SQLCMDPASSWORD environment variable instead.

On Linux and macOS, when used with the -G option without -U, -P specifies a file that contains an access token (v17.8+). The token file should be in UTF-16LE (no BOM) format.

Access tokens can be obtained via various methods. You must ensure the access token is correct byte-for-byte, because it's sent as-is. The following example command obtains an access token. The command uses the Azure CLI and Linux commands and saves it to a file in the proper format. If your system or terminal's default encoding isn't ASCII or UTF-8, you might need to adjust the iconv options. Be sure to carefully secure the resulting file and delete it when it's no longer required.

az account get-access-token --resource https://database.windows.net --output tsv | cut -f 1 | tr -d '\n' | iconv -f ascii -t UTF-16LE > /tmp/tokenFile

-S [protocol:]server[\instance_name][,port]

Specifies the instance of SQL Server to connect to. This option sets the sqlcmd scripting variable SQLCMDSERVER.

Specify server_name to connect to the default instance of SQL Server on that server computer. Specify server_name[\instance_name] to connect to a named instance of SQL Server on that server computer. If you don't specify a server computer, sqlcmd connects to the default instance of SQL Server on the local computer. This option is required when you run sqlcmd from a remote computer on the network.

protocol can be tcp (TCP/IP), lpc (shared memory), or np (named pipes).

If you don't specify a server_name[\instance_name] when you start sqlcmd, SQL Server checks for and uses the SQLCMDSERVER environment variable.

Note

The OSQLSERVER environment variable is kept for backward compatibility. The SQLCMDSERVER environment variable takes precedence over the OSQLSERVER environment variable. This means that sqlcmd and osql can be used next to each other without interference. Old scripts continue to work.

The ODBC driver on Linux and macOS requires -S. The only valid protocol value is tcp.

-U login_id

The login name or contained database user name. For contained database users, you must provide the database name option (-d).

Note

The OSQLUSER environment variable is kept for backward compatibility. The SQLCMDUSER environment variable takes precedence over the OSQLUSER environment variable. This means that sqlcmd and osql can be used next to each other without interference. Old scripts continue to work.

If you don't specify either the -U option or the -P option, sqlcmd tries to connect by using Windows Authentication mode. Authentication is based on the Windows account of the user who is running sqlcmd.

If you use the -U option with the -E option (described later in this article), an error message is generated. If the -U option is followed by more than one argument, an error message is generated and the program exits.

-z new_password

Change the password. Replace <oldpassword> with the old password, and <newpassword> with the new password.

sqlcmd -U someuser -P <oldpassword> -z <newpassword>
sqlcmd -U someuser -P <oldpassword> -z <newpassword>
sqlcmd -U someuser -P <oldpassword> -z <newpassword>

-Z new_password

Change the password and exit. Replace <oldpassword> with the old password, and <newpassword> with the new password.

sqlcmd -U someuser -P <oldpassword> -Z <newpassword>
sqlcmd -U someuser -P <oldpassword> -Z <newpassword>
sqlcmd -U someuser -P <oldpassword> -Z <newpassword>

Input and output options

-f codepage | i:codepage[,o:codepage] | o:codepage[,i:codepage]

Specifies the input and output code pages. The codepage number is a numeric value that specifies an installed Windows code page.

Code-page conversion rules:

  • If you don't specify a code page, sqlcmd uses the current code page for both input and output files, unless the input file is a Unicode file, in which case no conversion is required.

  • sqlcmd automatically recognizes both big-endian and little-endian Unicode input files. If you specify the -u option, the output is always little-endian Unicode.

  • If you don't specify an output file, the output code page is the console code page. This approach enables the output to be displayed correctly on the console.

  • Multiple input files are assumed to use same code page. Unicode and non-Unicode input files can be mixed.

Enter chcp at the command prompt to verify the code page of cmd.exe.

Note

On Linux, the codepage number is a numeric value that specifies an installed Linux code page (available since 17.5.1.1).

-i input_file[,input_file2...]

Identifies the file that contains a batch of Transact-SQL statements or stored procedures. You can specify multiple files that sqlcmd reads and processes in order. Don't use any spaces between file names. sqlcmd first checks to see whether all the specified files exist. If one or more files don't exist, sqlcmd exits. The -i option and the -Q/-q options are mutually exclusive.

Note

If you use the -i option followed by one or more extra parameters, you must use a space between the parameter and the value. This requirement is a known issue in sqlcmd (Go).

Path examples:

-i C:\<filename>
-i \\<Server>\<Share$>\<filename>
-i "C:\Some Folder\<file name>"

File paths that contain spaces must be enclosed in quotation marks.

You can use this option more than once:

sqlcmd -i <input_file1> -i <input_file2>
sqlcmd -i <input_file1> -i <input_file2>
sqlcmd -i <input_file1> -i <input_file2>

-o output_file

Identifies the file that receives output from sqlcmd.

If you specify -u, sqlcmd stores the output_file in Unicode format. If the file name isn't valid, sqlcmd generates an error message and exits. sqlcmd doesn't support concurrent writing of multiple sqlcmd processes to the same file. If this happens, consider the file output corrupted or incorrect. The -f option is also relevant to file formats. sqlcmd creates this file if it doesn't exist. sqlcmd overwrites a file of the same name from a previous session. The file specified here isn't the stdout file. If you specify a stdout file, sqlcmd doesn't use this file.

Path examples:

-o C:< filename>
-o \\<Server>\<Share$>\<filename>
-o "C:\Some Folder\<file name>"

File paths that contain spaces must be enclosed in quotation marks.

-r[0 | 1]

Redirects the error message output to the screen (stderr). If you don't specify a parameter or if you specify 0, only error messages that have a severity level of 11 or higher are redirected. If you specify 1, all error message output including PRINT is redirected. This option has no effect if you use -o. By default, messages are sent to stdout.

Note

For the sqlcmd (Go) utility, -r requires a 0 or 1 argument.

-R

Applies to: sqlcmd (ODBC) only.

Causes sqlcmd to localize numeric, currency, date, and time columns retrieved from SQL Server based on the client's locale. By default, these columns are displayed using the server's regional settings.

Note

On Linux and macOS, -R currently only uses en_US (US English) formatting.

-u

Specifies that output_file is stored in Unicode format, regardless of the format of input_file.

Note

For the sqlcmd (Go) utility, the generated Unicode output file is prefixed with the UTF-16 little-endian byte-order mark (BOM).

Query execution options

-e

Writes input scripts to the standard output device (stdout).

-I

Applies to: sqlcmd (ODBC) only.

Sets the SET QUOTED_IDENTIFIER connection option to ON. The default setting is OFF. For more information, see SET QUOTED_IDENTIFIER.

Note

To disable quoted identifier behavior in the sqlcmd (Go) utility, add SET QUOTED IDENTIFIER OFF in your scripts.

-q "cmdline query"

Executes a query when sqlcmd starts, but doesn't exit sqlcmd when the query finishes. You can run multiple queries delimited with a semicolon. Use quotation marks around the query, as shown in the following example.

At the command prompt, type:

sqlcmd -d AdventureWorks2025 -q "SELECT FirstName, LastName FROM Person.Person WHERE LastName LIKE 'Whi%';"
sqlcmd -d AdventureWorks2025 -q "SELECT TOP 5 FirstName FROM Person.Person;SELECT TOP 5 LastName FROM Person.Person;"
sqlcmd -d AdventureWorks2025 -q "SELECT FirstName, LastName FROM Person.Person WHERE LastName LIKE 'Whi%';"
sqlcmd -d AdventureWorks2025 -q "SELECT TOP 5 FirstName FROM Person.Person;SELECT TOP 5 LastName FROM Person.Person;"
sqlcmd -d AdventureWorks2025 -q "SELECT FirstName, LastName FROM Person.Person WHERE LastName LIKE 'Whi%';"
sqlcmd -d AdventureWorks2025 -q "SELECT TOP 5 FirstName FROM Person.Person;SELECT TOP 5 LastName FROM Person.Person;"

Important

Don't use the GO terminator in the query.

If you specify -b together with this option, sqlcmd exits with an error. The -b option is described elsewhere in this article.

-Q "cmdline query"

Executes a query when sqlcmd starts and then immediately exits sqlcmd. You can execute multiple queries delimited with a semicolon.

Use quotation marks around the query, as shown in the following example.

At the command prompt, type:

sqlcmd -d AdventureWorks2025 -Q "SELECT FirstName, LastName FROM Person.Person WHERE LastName LIKE 'Whi%';"
sqlcmd -d AdventureWorks2025 -Q "SELECT TOP 5 FirstName FROM Person.Person;SELECT TOP 5 LastName FROM Person.Person;"
sqlcmd -d AdventureWorks2025 -Q "SELECT FirstName, LastName FROM Person.Person WHERE LastName LIKE 'Whi%';"
sqlcmd -d AdventureWorks2025 -Q "SELECT TOP 5 FirstName FROM Person.Person;SELECT TOP 5 LastName FROM Person.Person;"
sqlcmd -d AdventureWorks2025 -Q "SELECT FirstName, LastName FROM Person.Person WHERE LastName LIKE 'Whi%';"
sqlcmd -d AdventureWorks2025 -Q "SELECT TOP 5 FirstName FROM Person.Person;SELECT TOP 5 LastName FROM Person.Person;"

Important

Don't use the GO terminator in the query.

If you specify -b together with this option, sqlcmd exits with an error. The -b option is described elsewhere in this article.

-t query_timeout

Specifies the number of seconds before a command (or Transact-SQL statement) times out. This option sets the sqlcmd scripting variable SQLCMDSTATTIMEOUT. If you don't specify a query_timeout value, the command doesn't time out. The query_timeout must be a number between 1 and 65534. If you provide a value that's not numeric or doesn't fall into that range, sqlcmd generates an error.

Note

The actual timeout value can vary from the specified query_timeout value by several seconds.

-v var = value [ var = value... ]

Applies to: Windows only. Linux and macOS aren't supported.

Creates a sqlcmd scripting variable for use in a sqlcmd script.

Enclose the value in quotation marks if the value contains spaces. You can specify multiple <var>="<value>" values. If any of the values you specify contain errors, sqlcmd generates an error message and then exits.

sqlcmd -v MyVar1=something MyVar2="some thing"
sqlcmd -v MyVar1=something -v MyVar2="some thing"
sqlcmd -v MyVar1=something MyVar2="some thing"
sqlcmd -v MyVar1=something -v MyVar2="some thing"
sqlcmd -v MyVar1=something MyVar2="some thing"
sqlcmd -v MyVar1=something -v MyVar2="some thing"

-x

Causes sqlcmd to ignore scripting variables. This parameter is useful when a script contains many INSERT statements that might contain strings that have the same format as regular variables, such as $(<variable_name>).

Format options

-h headers

Specifies the number of rows to print between the column headings. The default setting prints the headings once for each set of query results. This option sets the sqlcmd scripting variable SQLCMDHEADERS. Use -1 to specify that headers aren't printed. An invalid value causes sqlcmd to generate an error message and then exit.

-k [1 | 2]

Removes all control characters, such as tabs and new line characters from the output. This parameter preserves column formatting when data is returned.

  • -k removes control characters.
  • -k1 replaces each control character with a space.
  • -k2 replaces consecutive control characters with a single space.

-s col_separator

Specifies the column-separator character. The default is a blank space. This option sets the sqlcmd scripting variable SQLCMDCOLSEP. To use characters that have special meaning to the operating system, such as the ampersand (&) or semicolon (;), enclose the character in quotation marks ("). The column separator can be any 8-bit character.

-w screen_width

Specifies the screen width for output. This option sets the sqlcmd scripting variable SQLCMDCOLWIDTH. The column width must be a number greater than 8 and less than 65536. If the specified column width doesn't fall into that range, sqlcmd generates an error message. The default width is 80 characters. When an output line exceeds the specified column width, it wraps on to the next line.

-W

Removes trailing spaces from a column. Use this option together with the -s option when preparing data that you want to export to another application. Can't be used with the -y or -Y options.

-y variable_length_type_display_width

Sets the sqlcmd scripting variable SQLCMDMAXVARTYPEWIDTH. The default value is 256. It limits the number of characters that are returned for the large variable length data types:

  • varchar(max)
  • nvarchar(max)
  • varbinary(max)
  • xml
  • user-defined data types (UDTs)
  • text
  • ntext
  • image

UDTs can be of fixed length depending on the implementation. If this length of a fixed length UDT is shorter than display_width, the value of the UDT returned isn't affected. However, if the length is longer than display_width, the output is truncated.

Caution

Use the -y 0 option with extreme caution, because it can cause significant performance issues on both the server and the network, depending on the size of data returned.

-Y fixed_length_type_display_width

Sets the sqlcmd scripting variable SQLCMDMAXFIXEDTYPEWIDTH. The default value is 0 (unlimited). Limits the number of characters that are returned for the following data types:

  • char(n), where 1 <= n <= 8000
  • nchar(n), where 1 <= n <= 4000
  • varchar(n), where 1 <= n <= 8000
  • nvarchar(n), where 1 <= n <= 4000
  • varbinary(n), where 1 <= n <= 4000
  • sql_variant

Error reporting options

-b

Specifies that sqlcmd exits and returns a DOS ERRORLEVEL value when an error occurs. The value that sqlcmd returns to the ERRORLEVEL variable is 1 when the SQL Server error message has a severity level greater than 10. Otherwise, the value returned is 0. If you set the -V option, in addition to -b, sqlcmd doesn't report an error if the severity level is lower than the values set by the -V option. Command prompt batch files can test the value of ERRORLEVEL and handle the error appropriately. sqlcmd doesn't report errors for severity level 10 (informational messages).

If the sqlcmd script contains an incorrect comment, syntax error, or is missing a scripting variable, the ERRORLEVEL returned is 1.

-m error_level

Controls which error messages are sent to stdout. sqlcmd sends messages that have a severity level greater than or equal to this level. When you set this value to -1, sqlcmd sends all messages, including informational messages. Don't include spaces between the -m and -1. For example, -m-1 is valid, and -m -1 isn't.

This option also sets the sqlcmd scripting variable SQLCMDERRORLEVEL. This variable has a default of 0.

-V error_severity_level

Controls the severity level that sqlcmd uses to set the ERRORLEVEL variable. Error messages that have severity levels greater than or equal to this value set ERRORLEVEL. Values that are less than 0 are reported as 0. You can use batch and CMD files to test the value of the ERRORLEVEL variable.

Miscellaneous options

-a packet_size

Requests a packet of a different size. This option sets the sqlcmd scripting variable SQLCMDPACKETSIZE. packet_size must be a value between 512 and 32767. The default is 4096. A larger packet size can enhance performance for execution of scripts that have many Transact-SQL statements between GO commands. You can request a larger packet size. However, if the request is denied, sqlcmd uses the server default for packet size.

-c batch_terminator

Specifies the batch terminator. By default, you must terminate commands and send them to SQL Server using the word GO on a line by itself, followed by Enter. When you reset the batch terminator, don't use Transact-SQL reserved keywords or characters that have special meaning to the operating system, even if they're preceded by a backslash.

-L[c]

Applies to: Windows only. Linux and macOS aren't supported.

Lists the locally configured server computers, and the names of the server computers that are broadcasting on the network. You can't use this parameter in combination with other parameters. The maximum number of server computers that can be listed is 3,000. If the server list is truncated because of the size of the buffer, a warning message is displayed.

Note

Because of the nature of broadcasting on networks, sqlcmd might not receive a timely response from all servers. Therefore, the list of servers returned can vary for each invocation of this option.

If you specify the optional parameter c, the output appears without the Servers: header line, and each server line is listed without leading spaces. This presentation is referred to as clean output. Clean output improves the processing performance of scripting languages.

-p[1]

Prints performance statistics for every result set. The following display is an example of the format for performance statistics:

Network packet size (bytes): n

x xact[s]:

Clock Time (ms.): total       t1  avg       t2 (t3 xacts per sec.)

Where:

  • x = Number of transactions processed by SQL Server.
  • t1 = Total time for all transactions.
  • t2 = Average time for a single transaction.
  • t3 = Average number of transactions per second.

All times are in milliseconds.

If you specify the optional parameter 1, the output format of the statistics is in colon-separated format that can be imported easily into a spreadsheet or processed by a script.

If you specify the optional parameter as any value other than 1, an error is generated and sqlcmd exits.

-X[1]

Disables commands that might compromise system security when sqlcmd is executed from a batch file. The disabled commands are still recognized; sqlcmd issues a warning message and continues. If you specify the optional parameter 1, sqlcmd generates an error message and then exits. The following commands are disabled when the -X option is used:

  • ED
  • !! command

If you specify the -X option, it prevents environment variables from being passed on to sqlcmd. It also prevents the startup script specified by using the SQLCMDINI scripting variable from being executed. For more information about sqlcmd scripting variables, see sqlcmd - Use with scripting variables.

-?

Displays the version of sqlcmd and a syntax summary of sqlcmd options.

Note

On macOS, run sqlcmd '-?' (with quotation marks) instead.

Remarks

You don't have to use options in the order shown in the syntax section.

Note

If you use the -i option followed by one or more extra parameters, you must use a space between the parameter and the value. This requirement is a known issue in sqlcmd (Go).

sqlcmd prints a blank line between multiple result sets in a batch. In addition, the <x> rows affected message doesn't appear when it doesn't apply to the running statement.

To use sqlcmd interactively, type sqlcmd at the command prompt with any one or more of the options described earlier in this article. For more information, see Use sqlcmd.

Note

The options -l, -Q, -Z, or -i cause sqlcmd to exit after execution.

The underlying operating system determines the total length of the sqlcmd command line in the command environment (for example, cmd.exe or bash), including all arguments and expanded variables.

DSN support in sqlcmd and bcp

You can specify a data source name (DSN) instead of a server name in the sqlcmd or bcp -S option (or sqlcmd :Connect command) if you specify -D. If you use the -D option, sqlcmd and bcp connect to the server specified in the DSN by the -S option.

System DSNs are stored in the odbc.ini file in the ODBC SysConfigDir directory (/etc/odbc.ini on standard installations). User DSNs are stored in .odbc.ini in a user's home directory (~/.odbc.ini).

On Windows systems, System and User DSNs are stored in the registry and managed via odbcad32.exe. bcp and sqlcmd don't support file DSNs.

For a list of entries that the driver supports, see DSN and Connection String Keywords and Attributes.

In a DSN, only the DRIVER entry is required. To connect to a remote server, sqlcmd or bcp needs a value in the SERVER element. If the SERVER element is empty or not present in the DSN, sqlcmd or bcp attempt to connect to the default instance on the local system.

When you use bcp on Windows systems, SQL Server 2017 (14.x) and earlier versions require the SQL Native Client 11 driver (sqlncli11.dll), while SQL Server 2019 (15.x) and later versions require the Microsoft ODBC Driver 17 for SQL Server driver (msodbcsql17.dll).

If you specify the same option in both the DSN and the sqlcmd or bcp command line, the command line option overrides the value used in the DSN. For example, if the DSN has a DATABASE entry and the sqlcmd command line includes -d, the value passed to -d is used. If you specify Trusted_Connection=yes in the DSN, Kerberos authentication is used; user name (-U) and password (-P), if provided, are ignored.

You can modify existing scripts that invoke isql to use sqlcmd, by defining the following alias: alias isql="sqlcmd -D".

sqlcmd best practices

Use the following practices to help maximize security and efficiency:

  • Use integrated security.

  • Use -X[1] in automated environments.

  • Secure input and output files by using appropriate file system permissions.

  • To increase performance, do as much as possible in one sqlcmd session, instead of using a series of sessions.

  • Set timeout values for batch or query execution higher than the expected execution time for the batch or query.

Use the following practices to help maximize correctness:

  • Use -V 16 to log any severity 16 level messages. Severity 16 messages indicate general errors that you can correct.

  • Check the exit code and DOS ERRORLEVEL variable after the process exits. sqlcmd returns 0 normally. Otherwise, it sets the ERRORLEVEL as configured by -V. In other words, don't expect ERRORLEVEL to be the same value as the error number reported from SQL Server. The error number is a SQL Server-specific value corresponding to the system function @@ERROR. ERRORLEVEL is a sqlcmd-specific value to indicate why sqlcmd terminated. Its value is influenced by specifying the -b parameter.

Using -V 16 in combination with checking the exit code and DOS ERRORLEVEL can help catch errors in automated environments, particularly quality gates before a production release.