sqlcmd Utility
The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt, in Query Editor in SQLCMD mode, in a Windows script file or in an operating system (Cmd.exe) job step of a SQL Server Agent job. This utility uses ODBC to execute Transact-SQL batches.
Important
|
|---|
|
SQL Server Management Studio uses the Microsoft .NET Framework SqlClient for execution in regular and SQLCMD mode in Query Editor. When sqlcmd is run from the command line, sqlcmd uses the ODBC driver. Because different default options may apply, you might see different behavior when you execute the same query in SQL Server Management Studio in SQLCMD Mode and in the sqlcmd utility. |
Currently, sqlcmd does not require a space between the command line option and the value. However, in a future release, a space may be required between the command line option and the value.
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 codepage | i:codepage[,o:codepage] | o:codepage[,i:codepage] -h rows_per_header -H workstation_name -i input_file -I (enable quoted identifiers) -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 -N (encrypt connection) -o output_file -p[1] (print statistics, optional colon format) -P password -q "cmdline query" -Q "cmdline query" (and exit) -r[0 | 1] (msgs to stderr) -R (use client regional settings) -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 column_width -W (remove trailing spaces) -x (disable variable substitution) -X[1] (disable commands, startup script, environment variables and optional exit) -y variable_length_type_display_width -Y fixed_length_type_display_width -z new_password -Z new_password (and exit) -? (usage)
Options do not have to be used in the order shown in the syntax section.
When multiple results are returned, sqlcmd prints a blank line between each result set in a batch. In addition, the "<x> rows affected" message does not appear when it does not apply to the statement executed.
To use sqlcmd interactively, type sqlcmd at the command prompt with any one or more of the options described earlier in this topic. For more information, see Use the sqlcmd Utility
Note
|
|---|
|
The options -L, -Q, -Z or -i cause sqlcmd to exit after execution. |
The total length of the sqlcmd command line in the command environment (Cmd.exe), including all arguments and expanded variables, is that which is determined by the operating system for Cmd.exe.
|
Variable |
Related switch |
R/W |
Default |
|---|---|---|---|
|
SQLCMDUSER |
-U |
R |
"" |
|
SQLCMDPASSWORD |
-P |
-- |
"" |
|
SQLCMDSERVER |
-S |
R |
"DefaultLocalInstance" |
|
SQLCMDWORKSTATION |
-H |
R |
"ComputerName" |
|
SQLCMDDBNAME |
-d |
R |
"" |
|
SQLCMDLOGINTIMEOUT |
-l |
R/W |
"8" (seconds) |
|
SQLCMDSTATTIMEOUT |
-t |
R/W |
"0" = wait indefinitely |
|
SQLCMDHEADERS |
-h |
R/W |
"0" |
|
SQLCMDCOLSEP |
-s |
R/W |
" " |
|
SQLCMDCOLWIDTH |
-w |
R/W |
"0" |
|
SQLCMDPACKETSIZE |
-a |
R |
"4096" |
|
SQLCMDERRORLEVEL |
-m |
R/W |
0 |
|
SQLCMDMAXVARTYPEWIDTH |
-y |
R/W |
"256" |
|
SQLCMDMAXFIXEDTYPEWIDTH |
-Y |
R/W |
"0" = unlimited |
|
SQLCMDEDITOR |
|
R/W |
"edit.com" |
|
SQLCMDINI |
|
R |
"" |
SQLCMDUSER, SQLCMDPASSWORD and SQLCMDSERVER are set when :Connect
is used.
R indicates the value can only be set one time during program initialization.
R/W indicates that the value can be modified by using the setvar command and subsequent commands will be influenced by the new value.
In addition to Transact-SQL statements within sqlcmd, the following commands are also available:
|
GO [count] |
:List |
|
[:] RESET |
:Error |
|
[:] ED |
:Out |
|
[:] !! |
:Perftrace |
|
[:] QUIT |
:Connect |
|
[:] EXIT |
:On Error |
|
:r |
:Help |
|
:ServerList |
:XML [ON | OFF] |
|
:Setvar |
:Listvar |
Be aware of the following when you use sqlcmd commands:
-
All sqlcmd commands, except GO, must be prefixed by a colon (:).
Important
To maintain backward compatibility with existing osql scripts, some of the commands will be recognized without the colon. This is indicated by the [:].
-
sqlcmd commands are recognized only if they appear at the start of a line.
-
All sqlcmd commands are case insensitive.
-
Each command must be on a separate line. A command cannot be followed by a Transact-SQL statement or another command.
-
Commands are executed immediately. They are not put in the execution buffer as Transact-SQL statements are.
sqlcmd File Names
sqlcmd input files can be specified with the -i option or the :r command. Output files can be specified with the -o option or the :Error, :Out and :Perftrace commands. The following are some guidelines for working with these files:
-
:Error, :Out and :Perftrace should use separate <filename>. If the same <filename> is used, inputs from the commands may be intermixed.
-
If an input file that is located on a remote server is called from sqlcmd on a local computer and the file contains a drive file path such as :out c:\OutputFile.txt. The output file will be created on the local computer and not on the remote server.
-
Valid file paths include: C:\<filename>, \\<Server>\<Share$>\<filename> and "C:\Some Folder\<file name>". If there is a space in the path, use quotation marks.
-
Each new sqlcmd session will overwrite existing files that have the same names.
Informational Messages
sqlcmd prints any informational message that are sent by the server. In the following example, after the Transact-SQL statements are executed, an informational message is printed.
At the command prompt, type the following:
sqlcmd
At the sqlcmd prompt type:
USE AdventureWorks2012;
GO
When you press ENTER, the following informational message is printed: "Changed database context to 'AdventureWorks2012'."
Output Format from Transact-SQL Queries
sqlcmd first prints a column header that contains the column names specified in the select list. The column names are separated by using the SQLCMDCOLSEP character. By default, this is a space. If the column name is shorter than the column width, the output is padded with spaces up to the next column.
This line will be followed by a separator line that is a series of dash characters. The following output shows an example.
Start sqlcmd. At the sqlcmd command prompt, type the following:
USE AdventureWorks2012;
SELECT TOP (2) BusinessEntityID, FirstName, LastName
FROM Person.Person;
GO
When you press ENTER, the following result set is retuned.
BusinessEntityID FirstName LastName
---------------- ------------ ----------
285 Syed Abbas
293 Catherine Abel
(2 row(s) affected)
Although the BusinessEntityID column is only 4 characters wide, it has been expanded to accommodate the longer column name. By default, output is terminated at 80 characters. This can be changed by using the -w option, or by setting the SQLCMDCOLWIDTH scripting variable.
XML Output Format
XML output that is the result of a FOR XML clause is output, unformatted, in a continuous stream.
When you expect XML output, use the following command: :XML ON.
Note
|
|---|
|
sqlcmd returns error messages in the usual format. Notice that the error messages are also output in the XML text stream in XML format. By using :XML ON, sqlcmd does not display informational messages. |
To set the XML mode off, use the following command: :XML OFF.
The GO command should not appear before the XML OFF command is issued because the XML OFF command switches sqlcmd back to row-oriented output.
XML (streamed) data and rowset data cannot be mixed. If the XML ON command has not been issued before a Transact-SQL statement that outputs XML streams is executed, the output will be garbled. If the XML ON command has been issued, you cannot execute Transact-SQL statements that output regular row sets.
Note
|
|---|
|
The :XML command does not support the SET STATISTICS XML statement. |
Use the following practices to help maximize security and efficiency.
-
Use integrated security.
-
Use -X in automated environments.
-
Secure input and output files by using appropriate NTFS file system permissions.
-
To increase performance, do as much in one sqlcmd session as you can, instead of in a series of sessions.
-
Set time-out values for batch or query execution higher than you expect it will take to execute the batch or query.