Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 764
Jpin Avatar asked Feb 17 '15 12:02

Jpin


1 Answers

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

like image 167
Rudy Avatar answered Oct 29 '22 16:10

Rudy