是否可以在脚本中自动强制使用 SQLCMD 模式?

发布于 2024-09-13 11:43:04 字数 208 浏览 1 评论 0原文

我们使用的是 Visual Studio Database Professional,它大量使用 SQLCMD 变量来区分部署时的环境。

我知道有几个指令可用于设置上下文(例如:connect 用于服务器名称)。脚本本身是否有办法强制执行 SQLCMD 模式?我们部署过程的一部分是让 DBA 检查并执行脚本,这将是一个很好的安全网(因此我不必提醒他们将执行模式设置为 SQLCMD)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

别低头,皇冠会掉 2024-09-20 11:43:04

这不是一个解决方案,但作为一种解决方法,您可以将脚本嵌入到一些警告中。 这篇文章启发了我编写这段代码:

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

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
窗影残 2024-09-20 11:43:04

这似乎不可能。我什至检查了 SSMS 项目模式。

但是,如果您在 BIDS 中创建数据库项目,则预部署和部署过程将在 BIDS 中进行。默认情况下,部署后脚本在 SQLCMD 模式下运行。

我知道这不是您想要的答案,但这是我能给您的最好的答案,无需创建一个自定义 SSMS 插件,该插件将根据脚本文件中的某些文本为您完成此操作。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文