Because this is an SSMS option, not a TSQL option, you cannot directly test for it.
Please see:
https://jonlabelle.com/snippets/view/sql/check-if-sql-command-mode-is-enabled
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
We have a problem with DEV's running SQLCMD scripts in SSMS without SQLCMD mode turned on.
Code like this (but not this, obvs):
use PRODUCTION
:`setvar CurrentDatabase "DEVELOPMENT"
use [$(CurrentDatabase)]
truncate table Business.CriticalData
What I'd like is for a check to be made against SQLCMD mode, and the script terminated if it's not running.
Reading around, it seems no implementation of the following metacode is supported:
if @SQLCMDMODE = 0
exit
All of the solutions seem to rely on the user being an admin and don't work if they're not.
E.g. "RAISERROR 20" and "set noexec on"
This is such a fundamental requirement.
Is there really no way to do it?
Because this is an SSMS option, not a TSQL option, you cannot directly test for it.
Please see:
https://jonlabelle.com/snippets/view/sql/check-if-sql-command-mode-is-enabled
That does not work if you're not an admin.
Or, it just doesn't work at all.
This is so simple and necessary, there must be something in a bigger picture.
Further, since the whole script is pre-parsed by SSMS, why does it allow the script to run AT ALL if it's got SQLCMD syntax in it?
SSDT does this via this bit for publish scripts:
:setvar __IsSqlCmdEnabled "True"
GO
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
BEGIN
PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
SET NOEXEC ON;
END