Not logged in - Login

Ensure SQL Command mode is turned on

The following code snippet will ensure that the user has turned on SQL Command Mode before executing the script.

---------------------------------------------------------------------------------------------
-- This statement directs the execution of the script to stop if we encounter an error.
:on error exit
---------------------------------------------------------------------------------------------

GO

/*
-- Detect SQLCMD mode and disable script execution if SQLCMD mode is not supported.
-- To re-enable the script after enabling SQLCMD mode, execute the following:
SET NOEXEC OFF; 
*/

-- Attempt to set a SQL Command Mode variable.
:setvar __IsSqlCmdEnabled "True"
GO

-- Test to see if the SQL Command Mode variable is set
IF N'$(__IsSqlCmdEnabled)' NOT LIKE N'True'
BEGIN
    -- If we come in here, our SQL Command Mode variable is not set which 
    -- means we are not running in SQL Command Mode.
    -- Do what we need to do to stop execution of the script.
    PRINT N'SQLCMD mode must be enabled to successfully execute this script.';
    raiserror('SQLCMD mode must be enabled to successfully execute this script.', 20, -1) with log
    SET NOEXEC ON;
END

GO