- Login-Related Options
- -Ulogin_id
Is the user login ID.
Note |
|---|
The OSQLUSER environment variable is available 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. It also means that existing osql scripts will continue to work. |
If neither the -U option nor the -P option is specified, sqlcmd tries to connect by using Microsoft Windows Authentication mode. Authentication is based on the Windows account of the user who is running sqlcmd.
If the -U option is used with the -E option (described later in this topic), 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.
- -Ppassword
Is a user-specified password. Passwords are case sensitive. If the -U option is used and the -P option is not used, and the SQLCMDPASSWORD environment variable has not been set, sqlcmd prompts the user for a password. If the -P option is used at the end of the command prompt without a password sqlcmd uses the default password (NULL).
Security Note |
|---|
Do not use a blank password. Use a strong password. For more information, see Strong Passwords. |
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 do not 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:
SET SQLCMDPASSWORD= p@a$$w0rd
Security Note |
|---|
The password will be visible to anyone who can see your computer monitor. |
At the following command prompt, type:
sqlcmd
If the user name and password combination is incorrect, the OLE DB provider generates an error message.
Note |
|---|
The OSQLPASSWORD environment variable has been 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 and that old scripts will continue to work. |
If the -P option is used with the -E option, an error message is generated.
If the -P option is followed by more than one argument, an error message is generated and the program exits.
- -E trusted connection
Uses a trusted connection instead of using a user name and password to log on 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.
- -z new password
Change password:
sqlcmd -U someuser -P s0mep@ssword -z a_new_p@a$$w0rd
- -Z new password and exit
Change password and exit:
sqlcmd -U someuser -P s0mep@ssword -Z a_new_p@a$$w0rd
- -Sserver_name [ \instance_name ]
Specifies the instance of SQL Server to which to connect. It 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 no server computer is specified, sqlcmd connects to the default instance of SQL Server on the local computer. This option is required when you execute sqlcmd from a remote computer on the network.
If you do not 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 has been 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 and that old scripts will continue to work. |
- -Hwksta_name
Is a workstation name. This option sets the sqlcmd scripting variable SQLCMDWORKSTATION. The workstation name is listed in the hostname column of the sys.processes catalog view and can be returned using the stored procedure sp_who. If this option is not specified, the default is the current computer name. This name can be used to identify different sqlcmd sessions.
- -ddb_name
Issues a USE db_name statement when you start sqlcmd. This option sets the sqlcmd scripting variable SQLCMDDBNAME. This specifies the initial database. The default is your login's default-database property. If the database does not exist, an error message is generated and sqlcmd exits.
- -llogintime_out
Specifies the number of seconds before a sqlcmd login to the OLE DB provider 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 eight seconds. The login time-out must be a number between 0 and 65534. If the value supplied is not numeric or does not fall into that range, sqlcmd generates an error message. A value of 0 specifies time-out to be infinite.
- -A dedicated admin connection
Logs in to SQL Server with a Dedicated Administrator Connection (DAC). This kind of connection is used to troubleshoot a server. This will only work with server computers that support DAC. If DAC is not available, sqlcmd generates an error message and then exits. For more information about DAC, see Using a Dedicated Administrator Connection.
- Input/Output Options
- -i input_file[,input_file2...]
Identifies the file that contains a batch of SQL statements or stored procedures. Multiple files may be specified that will be read and processed in order. Do not use any spaces between file names. sqlcmdwill first check to see whether all the specified files exist. If one or more files do not exist, sqlcmd will exit. The -i and the -Q/-q options are mutually exclusive.
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.
This option may be used more than once: -i input_file -i I input_file.
- -ooutput_file
Identifies the file that receives output from sqlcmd.
If -u is specified, the output_file is stored in Unicode format. If the file name is not valid, an error message is generated, and sqlcmd exits. sqlcmd does not support concurrent writing of multiple sqlcmd processes to the same file. The file output will be corrupted or incorrect. See the -f switch for more information about file formats. This file will be created if it does not exist. A file of the same name from a prior sqlcmd session will be overwritten. The file specified here is not the stdout file. If a stdout file is specified this file will not be used.
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.
- -f < codepage > | i: < codepage > [ <, o: < codepage > ]
Specifies the input and output code pages. The codepage number is a numeric value that specifies an installed Windows code page. For more information, see Collation Settings in Setup.
Code-page conversion rules:
If no code pages are specified, sqlcmd will use 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 the -u option has been specified, the output will always be little-endian Unicode.
If no output file is specified, the output code page will be the console code page. This enables the output to be displayed correctly on the console.
Multiple input files are assumed to be of the 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.
- -u unicode output
Specifies that output_file is stored in Unicode format, regardless of the format of input_file.
- -r[ 0 | 1] msgs to stderr
Redirects the error message output to the screen (stderr). If you do not 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. Has no effect if you use -o. By default, messages are sent to stdout.
- -R use client regional settings
Sets the SQL Server OLE DB provider to use client regional settings when it converts currency, and date and time data to character data. The default is server regional settings.
- Query Execution Options
- -q" cmdline query "
Executes a query when sqlcmd starts, but does not exit sqlcmd when the query has finished running. Multiple-semicolon-delimited queries can be executed. Use quotation marks around the query, as shown in the following example.
At the command prompt, type:
sqlcmd -d AdventureWorks -q "SELECT FirstName, LastName FROM Person.Contact WHERE LastName LIKE 'Whi%';"
sqlcmd -d AdventureWorks -q "SELECT TOP 5 FirstName FROM Person.Contact;SELECT TOP 5 LastName FROM Person.Contact;"
Important |
|---|
Do not use the GO terminator in the query. |
If -b is specified together with this option, sqlcmd exits on error. -b is described later in this topic.
- -Q"cmdline query " and exit
Executes a query when sqlcmd starts and then immediately exits sqlcmd. Multiple-semicolon-delimited queries can be executed.
Use quotation marks around the query, as shown in the following example.
At the command prompt, type:
sqlcmd -d AdventureWorks -Q "SELECT FirstName, LastName FROM Person.Contact WHERE LastName LIKE 'Whi%';"
sqlcmd -d AdventureWorks -Q "SELECT TOP 5 FirstName FROM Person.Contact;SELECT TOP 5 LastName FROM Person.Contact;"
Important |
|---|
Do not use the GO terminator in the query. |
If -b is specified together with this option, sqlcmd exits on error. -b is described later in this topic.
- -e echo input
Writes input scripts to the standard output device (stdout).
- -I enable Quoted Identifiers
Sets the SET QUOTED_IDENTIFIER connection option to ON. By default, it is set to OFF. For more information, see SET QUOTED_IDENTIFIER (Transact-SQL).
- -tquerytime_out
Specifies the number of seconds before a command (or SQL statement) times out. This option sets the sqlcmd scripting variable SQLCMDSTATTIMEOUT. If a time_out value is not specified, the command does not time out. The querytime_out must be a number between 1 and 65535. If the value supplied is not numeric or does not fall into that range, sqlcmd generates an error message.
Note |
|---|
The actual time out value may vary from the specified time_out value by several seconds. |
- -vvar=value[ var=value...]
Creates a sqlcmdscripting variable that can be used in a sqlcmd script. Enclose the value in quotation marks if the value contains spaces. You can specify multiple var="values" values. If there are errors in any of the values specified, sqlcmd generates an error message and then exits.
sqlcmd -v MyVar1=something MyVar2="some thing"
sqlcmd -v MyVar1=something -v MyVar2="some thing"
- -x disable variable substitution
Causes sqlcmd to ignore scripting variables. This is useful when a script contains many INSERT statements that may contain strings that have the same format as regular variables, such as $(variable_name).
- Formatting Options
- -hheaders
Specifies the number of rows to print between the column headings. The default is to print headings one time for each set of query results. This option sets the sqlcmd scripting variable SQLCMDHEADERS. Use -1 to specify that headers must not be printed. Any value that is not valid causes sqlcmd to generate an error message and then exit.
- -scol_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.
- -wcolumn_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 does not fall into that range, sqlcmd generates and 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 remove trailing spaces
This option removes trailing spaces from a column. Use this option together with the -s option when preparing data that is to be exported to another application. Cannot be used with the -y or -Y options.
- -k[ 1 | 2 ] remove[replace] control characters
Removes all control characters, such as tabs and new line characters from the output. This preserves column formatting when data is returned. If 1 is specified, the control characters are replaced by a single space. If 2 is specified, consecutive control characters are replaced by a single space.
- -ydisplay_width
Sets the sqlcmd scripting variable SQLCMDMAXFIXEDTYPEWIDTH. The default = 0 (not set). It limits the number of characters that are returned for the large variable length data types:
Note |
|---|
UDTs can be of fixed length depending on the implementation. If this length of a fixed length UDT is shorter that display_width, the value of the UDT returned is not affected. However, if the length is longer than display_width, the output is truncated. |
If display_width is 0, the output is truncated at 1 MB. You can use the :XML ON command to prevent truncation of the output. The :XML ON command is described later in this topic.
Important |
|---|
Use the -y 0 option with extreme caution because it may cause serious performance issues on both the server and the network, depending on the size of data returned. |
- -Ydisplay_width
Sets the sqlcmd scripting variable SQLCMDMAXVARTYPEWIDTH. The default is 256. Limits the number of characters that are returned for the following data types:
- Error Reporting Options
- -b on error batch abort
Specifies that sqlcmd exits and returns a DOS ERRORLEVEL value when an error occurs. The value that is returned to the DOS ERRORLEVEL variable is 1 when the SQL Server error message has a severity level greater than 10; otherwise, the value returned is 0. If the -V option has been set in addition to -b, sqlcmd will not report an error if the severity level is lower than the values set using -V. Command prompt batch files can test the value of ERRORLEVEL and handle the error appropriately. sqlcmd does not report errors for severity level 10 (informational messages).
If the sqlcmd script contains an incorrect comment, syntax error, or is missing a scripting variable, ERRORLEVEL returned is 1.
- -V severitylevel
Controls the severity level that is used to set the ERRORLEVEL variable. Error messages that have severity levels less than or equal to this value set ERRORLEVEL. Values that are less than 0 are reported as 0. Batch and CMD files can be used to test the value of the ERRORLEVEL variable.
- -merror_level
Controls which error messages are sent to stdout. Messages that have a severity level less than or equal to this level are sent. When this value is set to -1, all messages including informational messages, are sent. Spaces are not allowed between the -m and -1. For example, -m-1 is valid, and -m-1 is not.
This option also sets the sqlcmd scripting variable SQLCMDERRORLEVEL. This variable has a default of 0.
- Miscellaneous Options
- -apacket_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 = 4096. A larger packet size can enhance performance for execution of scripts that have lots of 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.
- -ccmd_end
Specifies the batch terminator. By default, commands are terminated and sent to SQL Server by typing the word "GO" on a line by itself. When you reset the batch terminator, do not use Transact-SQL reserved keywords or characters that have special meaning to the operating system, even if they are preceded by a backslash.
- -L [ c ] list servers[clean output]
Lists the locally configured server computers, and the names of the server computers that are broadcasting on the network. This parameter cannot be used in combination with other parameters. The maximum number of server computers that can be listed is 3000. 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 may not receive a timely response from all servers. Therefore, the list of servers returned may vary for each invocation of this option. |
If the optional parameter c is specified, the output appears without the Servers: header line and each server line is listed without leading spaces. This is referred to as clean output. Clean output improves the processing performance of scripting languages.
- -p[ 1 ] print statistics[colon format]
Prints performance statistics for every result set. The following 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 that are 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 the optional parameter 1 is specified, 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 the optional parameter is any value other than 1, an error is generated and sqlcmd exits.
- -X [ 1 ] disable commands, startup script, enviroment variables [and exit]
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 the optional parameter 1 is specified, sqlcmd generates an error message and then exits. The following commands are disabled when the -X option is used:
If the -X option is specified, 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 Using sqlcmd with Scripting Variables.
- -? show syntax summary
Displays the syntax summary of sqlcmd options.