简体   繁体   中英

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.
The condition is the servername ( @@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.


The Problem:
As there are many GO -statements, how do i get the condition to work? When doing it like this, i guess the GO statements inside those PostDeployMasterScripts will break the IF -statement?

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.


The needed solution: (tl;dr)
What is the best way to implement the given condition and execute my scripts and not break the IF -statement? (postdeploy, additional project,...?)

Have your IF check in a little batch that then sets PARSEONLY to 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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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