Skip to main content
Feb 12, 2017

What is 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.

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.

Syntax

Here is the syntax for the sqlcmd command listed with its arguments (parameters)

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

-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)

Remember that the option -? displays the syntax summary of sqlcmd options.

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.

The usage of the sqlcmd can be read on Microsoft site: http://msdn.microsoft.com/en-us/library/ms180944.aspx