SQLCMD Utility

From SQLServerPedia

Jump to: navigation, search
See Also: Main_Page - Database Administration - Database Maintenance


SQLCMD replaces deprecated command line utilities ISQL (which is no longer available in SQL Server 2005) and OSQL. The latter is still supported but will be discontinued in future releases of software. You can use SQLCMD to run Transact-SQL statements, system procedures and scripts from the command line if client tools are not available. SQLCMD uses OLEDB for connecting to the server and executing statements. SQLCMD is available under SQL Server installation folder, by default this is C:\Program Files\Microsoft SQL Server\90\Tools\Binn. The following table explains SQLCMD switches; as with all other command-line tools switches are case sensitive:

Switch Explanation
-U Login ID. If -U is not specified SQL Server will attempt using Windows authentication.
-P Password of the login specified with -U switch.
-E Advises SQLCMD to use trusted connection (Windows authentication).
-z Allows changing the current login's password. Current password must also be supplied.
-Z Allows changing the password and immediately exits SQLCMD session.
-S Server name and instance name. You can specify instance name or port number. If you specify the port number instead of instance name separate server name and port number with a comma. If no instance name (or port number) is specified SQLCMD will attempt connecting to the default instance.
-H Sets workstation name. This switch is optional. If not specified SQLCMD assumes the current computer name. Workstation name appears under host_name column of sys.dm_exec_sessions view.
-d Database name to connect to. If not specified SQLCMD will connect to the default database of the specified login.
-l Login timeout in seconds - number of seconds to wait until the connection attempt times out.
-A Advises SQLCMD to use dedicated admin connection.
-i Full path to the input file containing the statements you wish to execute. You can specify -i several times to submit multiple input files. Requests will be submitted sequentially in the specified order. If the file name contains spaces you must enclose it in quotes.
-o Full path to the output file if submitted statements generate output. If the file name contains spaces you must enclose it in quotes.
-f Specifies code page for input or output files. If not specified current server code page is used.
-u Advises SQLCMD to store output file in Unicode format.
-r 0 or 1 Redirects error messages to the screen instead of output file. Value of 0 will redirect error messages with severity of 11 and higher. Value of 1 will redirect all error messages.
-R Uses client computer's regional settings when converting data such as date and time. By default SQLCMD will use server's regional settings.
-q Submits queries on the command line (instead of using an input file). You can delimit multiple statements with semi-colon to execute in the same batch.
-Q Submits one batch of command line queries (separated by semicolons) and immediately exits the SQLCMD session.
-e Echo input - write the input statements to the output files.
-I Executes SET_QUOTED_IDENTIFIER ON to enable quoted identifiers.
-t Query timeout value specified in seconds.
-v Allows defining scripting variables for statements executed through SQLCMD.
-x Disables variable substitution.
-h Number of rows to print between column headings.
-s Column separator. Default is a blank space.
-w Specifies screen width for output; valid values are between 8 and 65536 characters.
-W Removes trailing spaces from a column.
-k 1 or 2 Removes or replace control characters such as tabs and new line characters. 1 = remove, 2 = replace with a single space.
-y Limits the number of characters returned by variable length columns with VARCHAR(MAX), VARBINARY(MAX), NVARCHAR(MAX), TEXT, NTEXT, IMAGE, XML as well as user-defined data types. Default is 0 = unlimited.
-Y Limits the number of characters returned by character length columns with VARCHAR, VARBINARY, NCHAR, NVARCHAR, and SQL_VARIANT data types. Default is 256.
-b Advises SQLCMD to exit if an error occurs.
-V Specifies the lowest severity level of the error that SQLCMD will report. Errors will be reported only if their severity is greater than or equal to the value specified by this switch.
-m Customizes display of error messages. Value of -1 will display all headers with the messages.
-a Specifies network packet size. Default value is 4096.
-c Specifies a custom command terminator. By default batches are separated with GO statement.
-L Lists the locally configured servers. Additional option "c" can be specified with -L to remove "Servers:" header line. Note that SQL Browser server must be running before you can detect existence of the server.
-p Prints performance statistics for each result set.
-? Displays summary of SQLCMD syntax


For example, the following command exports the content of sys.dm_exec_query_stats into a file:

C:\Program Files\Microsoft SQL Server\90\Tools\Binn>sqlcmd.exe -S my_server\my_instance -Q   "SELECT * FROM sys.dm_exec_query_stats" -o c:\query_exec_stats.txt


The next statement lists all local servers:

C:\Program Files\Microsoft SQL Server\90\Tools\Binn>sqlcmd.exe -L