Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is there something like a directive which I can use in a script to force SSMS to enable/disable SQLCMD mode?

share|improve this question
up vote 11 down vote accepted

As has been said there isn't a way, however one thing we do is to include in our script header a quick check to determine whether SQLCMD mode is on (and terminate the script if not):

:setvar DatabaseName "MyDatabase"

IF ('$(DatabaseName)' = '$' + '(DatabaseName)')
    RAISERROR ('This script must be run in SQLCMD mode.', 20, 1) WITH LOG

-- If we got here then SQLCMD mode is on.
share|improve this answer
For ssms the first 2 lines would be enough and for sql 2000 Query Analyzer it works, but the message is not displayed. – bernd_k Sep 8 '11 at 6:07
But wouldn't the script just continue executing from the next GO? – Danere Sep 8 '11 at 23:34
I'm using SSMS 2008 R2 and there the 2 lines produce: Meldung 102, Ebene 15, Status 1, Zeile 1 Incorrect syntax near ':'. Using SQL Server 2000 Query Analyzer you need to use RAISERROR. – bernd_k Sep 9 '11 at 5:19
As mentioned here, you can also set SET NOEXEC ON as an added safety measure to prevent anything else in the script from running. – Yahoo Serious Apr 16 '13 at 10:25


But you can always run in SQLCMD mode and have T-SQL in it though

To make a clear distinction between SQLCMD commands and Transact-SQL, all SQLCMD commands, need to be prefixed with a colon (:).

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.