October 19, 2016 by Kenneth Fisher
For anyone who wasn’t aware SQLCMD is a command line tool for running T-SQL statements and scripts. Before anyone says anything I want to point out that yes, I realize that you can use Powershell instead. Of course not all of us know Powershell all that well and most of us do know T-SQL. Until that changes (and I expect it will) SQLCMD (or some form of it) will be around.
What are some common uses for SQLCMD?
- Run scripts on remote servers.
SQLCMD can easily be used to run a script or stored procedure on a remote SQL Server. Combining this with a bat file (yes, again older technology) and an Enterprise scheduler (because not all of us use SQL Server Agent) can be a very powerful way to run T-SQL.
- Connecting to the DAC
The DAC (dedicated admin connection) is vitally important when you have problems with your SQL Server. It has its own dedicated resources and can be used even when no other connections may be able to do anything. SQLCMD is a light weight way of connecting and fixing your server.
SQLCMD is installed with SSMS and with the database engine. This means it’s going to be installed on the server where your instance is located and probably your workstation as well. However every now and again you want it installed on a machine where no SQL components are installed. Say an application server. It’s not overly difficult fortunately. You just download and install the Command Line Utilities for SQL Server.