12

We're using Visual Studio Database Professional and it makes heavy use of SQLCMD variables to differentiate between environments while deploying.

I know there's several directives available for setting context (like :connect for server name). Is there a way within the script itself to force SQLCMD mode for execution? Part of our deployment process is to have DBA's examine and execute the scripts and it would be a nice safety net (so I don't have to remind them to set their execution mode to SQLCMD).

2
  • +1 Looking for a similar solution. Ever find out how to do it? Commented Nov 12, 2010 at 16:17
  • No, still waiting. I'm hesitant to start a bounty. Commented Feb 14, 2011 at 20:49

2 Answers 2

4

Not a solution, but as a work-around, you could embed your script in some warning. This post inspired me to this code:

SET NOEXEC OFF; -- previous execution may have toggled it
:setvar IsSqlCmdEnabled "True"
GO
IF ('$(IsSqlCmdEnabled)' = '$' + '(IsSqlCmdEnabled)')
BEGIN
  PRINT('Use SqlCmd-mode!!');
  SET NOEXEC ON;
  -- RAISERROR ('This script must be run in SQLCMD mode.', 20, 1) WITH LOG
END
ELSE
BEGIN
  PRINT('Using SqlCmd-mode')
  -- insert the code you really want to execute:
  -- ...
END
SET NOEXEC OFF; -- do not disable next execution in this session
Sign up to request clarification or add additional context in comments.

3 Comments

I know this is late but for anyone else who finds this in the future: the VS2010 publish scripts do basically this (they set a special __IsSqlCmdEnabled var then immediately check it's value) but they also SET NOEXEC ON as an added safety measure to prevent anything else in the script from running.
@Micheal, thanks for the tip, I added the NOEXEC. (Untested, no SqlServer around at this moment, but it looks simple enough.)
@Paul: that's because NOEXEC is still on for this session, and commands (inc. printing) will not be executed. If you end (and/or start) the command sequence with SET NOEXEC OFF it will repeat. To save people the trouble of having to type that (and think of that), I will also add that to the example.
0

This does not seem to be possible. I even checked the SSMS project mode.

However, if you create a database project in BIDS, the pre-deploy & post-deploy scripts run in SQLCMD mode by default.

I know that is not the answer you want, but it is the best I can give you w/o resorting creating a custom SSMS plugin that would do it for you based on some text in the script file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.