SQLCMD Command line options September 18th, 2013

Vinod Kumar

Recently I was reading the blog from Pinal around Execute Operating System Commands in sqlcmd and was wondering do we know all the command line parameters available with SQLCMD? Personally for me this is a very handy utility and I have used it in the past like anything.

The sqlcmd utility lets you enter Transact-SQL statements, system procedures, and script files at the command prompt. This utility replaces the osql command-line tool with many scripting enhancements. Some of the common command-line options are:




Logs in to SQL Server with a Dedicated Administrator Connection (DAC).


Issues a USE  db_name statement when you start sqlcmd. This option sets the sqlcmd scripting variable SQLCMDDBNAME.


Uses a trusted connection instead of using a user name and password to log on to SQL Server.


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.


This switch is used by the client to request an encrypted connection.


Identifies the file that receives output from sqlcmd.


Is a user-specified password. Passwords are case sensitive.

-q "cmd"

Executes a query when sqlcmd starts, but does not exit sqlcmd when the query has finished running. Multiple-semicolon-delimited queries can be executed.


Executes a query when sqlcmd starts and then immediately exits sqlcmd.


Value will be used to give a query timeout value.


Is the user login ID. If neither the -U option nor the -P option is specified, sqlcmd tries to connect by using Windows Authentication mode.


Changes Password and exits sqlcmd

This is not an exhaustive list of parameters that you can find but these are the most commonly used.

As I wrap up, wanted to call out one specific parameter which is –K with sqlcmd. When I was writing the book on AlwaysOn Availability Groups” (Paperback, Kindle), we wanted to show quickly how client redirection works with ApplicationIntent=ReadOnly and this handy parameter helps you achieve the same from sqlcmd.

Tags: , , , , , , , ,

This entry was posted on Wednesday, September 18th, 2013 at 08:30 and is filed under Technology. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Responses to “SQLCMD Command line options”

  1. Ramu says:

    I am great fan of you sir, I am keep on following your articles sir..

Leave a Reply