简体   繁体   English

仅在满足条件时执行许多脚本,而不通过 GO 语句中断 IF

[英]Execute many scripts only if condition is met, without breaking IF through GO-statement

In my SSDT Project (Visual Studio Database Project) i have a lot of scripts that must be executed only if a condition is met.在我的 SSDT 项目(Visual Studio 数据库项目)中,我有很多脚本,只有在满足条件时才必须执行。
The condition is the servername ( @@servername != 'LiveServer' ).条件是服务器名称 ( @@servername != 'LiveServer' )。
The scripts will do various things like create tables/stored procedures/jobs, fetch data from elsewhere etc.脚本将执行各种操作,例如创建表/存储过程/作业、从其他地方获取数据等。
As there are many things that will be executed, there are many GO -statements in those scripts.由于要执行的事情很多,因此这些脚本中有很多GO语句。


The Problem:问题:
As there are many GO -statements, how do i get the condition to work?由于有许多GO -statements,我如何让条件工作? When doing it like this, i guess the GO statements inside those PostDeployMasterScripts will break the IF -statement?当这样做时,我猜这些PostDeployMasterScripts中的GO语句会破坏IF语句?

IF (@@SERVERNAME != 'LiveServer')
print CONVERT(varchar, sysdatetime(), 8) + ' executing linked scripts'
:r .\PostDeployMasterScripts\Linked\CreateTemporaryTables.sql
:r .\PostDeployMasterScripts\Linked\CreateProcedures.sql
:r .\PostDeployMasterScripts\Linked\FetchInitialData.sql
:r .\PostDeployMasterScripts\Linked\CreateJobs.sql
print CONVERT(varchar, sysdatetime(), 8) + ' linked postdeploy finished'
GO

I was thinking about moving all of these postdeploy-scripts into a separate project in my solution, but i'm not sure if that would fix the issue, even if and when i find a way to only execute the scripts under the given condition.我正在考虑在我的解决方案中将所有这些 postdeploy-scripts 移动到一个单独的项目中,但我不确定这是否能解决问题,即使当我找到一种仅在给定条件下执行脚本的方法时也是如此。


The needed solution: (tl;dr)所需的解决方案: (tl; dr)
What is the best way to implement the given condition and execute my scripts and not break the IF -statement?实现给定条件并执行我的脚本而不破坏IF语句的最佳方法是什么? (postdeploy, additional project,...?) (部署后,附加项目,...?)

Have your IF check in a little batch that then sets PARSEONLY to ON .让您的IF检查一小批,然后将PARSEONLY设置为ON

Every subsequent batch will not be executed, until it hits another SET PARSEONLY statement (yes, the documentation doesn't actually mention that SET statements are exempt from the "don't compile and execute"-ness of PARSEONLY )每个后续批次都不会被执行,直到它遇到另一个SET PARSEONLY语句(是的,文档实际上并没有提到SET语句不受PARSEONLY的“不编译和执行”-ness 的约束)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM