简体   繁体   中英

SSDT Post Deployment Script - Run All Scripts In Folder

I have a PostDeployment script in a SQL project which runs a number of other scripts in the project when published:

:r .\Scripts\Script1.sql 
:r .\Scripts\Script2.sql 
:r .\Scripts\Script3.sql 
:r .\Scripts\Script4.sql

Rather than having to manually update this each time a new script is added to the Scripts folder in source control (TFS) is it possible to just iterate and execute all the SQL scripts within the Scripts folder?

Add this to your project file

 <ItemGroup>
    <DataScripts Include="Data\*.sql" />
 </ItemGroup>
 <Target AfterTargets="BeforeBuild" Name="CreateDataScript">
    <Delete Files="DataScript.sql" />
    <WriteLinesToFile Overwrite="false" File="DataScript.sql" Lines=":r .\%(DataScripts.Identity)" />
 </Target>

The DataScripts property is the folder with all of the sql scripts you want to run. This generates the file before you do the build so you can reference it in the post deployment script without issue like so.

:r .\DataScript.sql

This can be used to dynamically generate scripts that can be referenced in pre / post deployment scripts

It is not really an execution, but an inclusion of script files. So you must edit the file to add rows for all files. This could perhaps be done using xmlpoke command. You would have to edit the Project file and add code that loops all files in a folder and adds an include-statement for each.

If you need to keep manipulating this then you are likely using SSDT wrong. SSDT is a schema based versioning system not a script based system.

If you want a traditional script system then you should look to Ready Roll or SSW SQL Deploy.

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