Sunday, January 02, 2011

SSMS SQLCMD Mode

A question was posted on the MSDN forums in regards to executing a query file from SQL Server management studio without having to first open the file.  My first instinct was to use xp_cmdshell or even create a clr procedure, but this was way overkill.  I began to respond pointing out that the ability to pass an input query file using sqlcmd was easy and straightforward, but the post was specific to SSMS.  After a quick refresher I realized that using sqlcmd mode in management studio met all the requirements.
In SQL Server 2005 the command line interface sqlcmd was introduced as successor to osql.  In addition to the new interface sqlcmd mode was included in management studio which provides the ability to run queries and command line arguments in the same query window.  In order to take advantage of this feature then sqlcmd mode must be enabled for each query, unless this has been set as the default option for new queries.  To enable sqlcmd mode simply click the icon on the menu bar:
image_thumb%5B6%5D.png?imgmax=800

Read more: SQL Server pedia