Skip to content

Commit 27b2663

Browse files
authored
Merge pull request #35982 from rwestMSFT/rw-1202-fix-10238
Refresh Pre-Deployment and Post-Deployment Scripts (PR 10238)
2 parents 8511292 + ab831aa commit 27b2663

1 file changed

Lines changed: 30 additions & 15 deletions

File tree

docs/tools/sql-database-projects/concepts/pre-post-deployment-scripts.md

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
---
2-
title: Pre- and Post-Deployment Scripts
2+
title: Pre-Deployment and Post-Deployment Scripts
33
description: "Add custom scripts for pre/post-deployment execution."
44
author: dzsquared
55
ms.author: drskwier
66
ms.reviewer: maghan, randolphwest
7-
ms.date: 08/30/2024
7+
ms.date: 12/02/2025
88
ms.service: sql
99
ms.subservice: sql-database-projects
1010
ms.topic: concept-article
@@ -15,11 +15,11 @@ ms.custom:
1515
zone_pivot_groups: sq1-sql-projects-tools
1616
---
1717

18-
# Pre- and post-deployment scripts overview
18+
# Pre-deployment and post-deployment scripts overview
1919

2020
[!INCLUDE [SQL Server Azure SQL Database Azure SQL Managed Instance FabricSQLDB](../../../includes/applies-to-version/sql-asdb-asdbmi-fabricsqldb.md)]
2121

22-
Pre- and post-deployment scripts are SQL scripts that are included in the project to be executed during deployment. Pre/post-deployment scripts are included in the `.dacpac` but they aren't compiled into or validated with database object model. A pre-deployment script is executed before the deployment plan is executed but the deployment plan is calculated before the script executes. A post-deployment script is executed after the deployment plan completes.
22+
Pre-deployment and post-deployment scripts are SQL scripts that are included in the project to be executed during deployment. Pre/post-deployment scripts are included in the `.dacpac` but they aren't compiled into or validated with database object model. A pre-deployment script is executed before the deployment plan is executed but the deployment plan is calculated before the script executes. A post-deployment script is executed after the deployment plan completes.
2323

2424
## SQL project file sample and syntax
2525

@@ -44,14 +44,14 @@ The following example from a SQL project file adds the file `populate-app-settin
4444
</Project>
4545
```
4646

47-
Multiple files can be executed as part of a pre- or post-deployment script by using a SQLCMD script that calls each file in order.
47+
Multiple files can be executed as part of a pre-deployment or post-deployment script by using a SQLCMD script that calls each file in order.
4848

4949
```sql
5050
:r .\scripts\script1.sql
5151
:r .\scripts\script2.sql
5252
```
5353

54-
Those files should be excluded from the database model build by setting the `Build Action` property to `Remove` in the file properties in Visual Studio or by adding an entry for the file in the `.sqlproj` file with the `Build` attribute set to `Remove`. When the SQL project is built, the additional files are combined into their referencing pre-deployment or post-deployment script in the `.dacpac` by the Microsoft.Build.Sql project SDK.
54+
Those files should be excluded from the database model build by setting the `Build Action` property to `Remove` in the file properties in Visual Studio or by adding an entry for the file in the `.sqlproj` file with the `Build` attribute set to `Remove`. When the SQL project is built, the extra files are combined into their referencing pre-deployment or post-deployment script in the `.dacpac` by the `Microsoft.Build.Sql` project SDK.
5555

5656
```xml
5757
...
@@ -62,38 +62,53 @@ Those files should be excluded from the database model build by setting the `Bui
6262
</Project>
6363
```
6464

65-
> [!TIP]
65+
In SDK-style SQL projects, adding a `Build Remove="path\file.sql"` entry removes the file from the project entirely, which causes it to no longer appear in the Visual Studio Code project view. To keep the file visible while still excluding it from model compilation, add it again as a `None` item:
66+
67+
```xml
68+
...
69+
<ItemGroup>
70+
<Build Remove="scripts\script1.sql" />
71+
<None Include="scripts\script1.sql" />
72+
<Build Remove="scripts\script2.sql" />
73+
<None Include="scripts\script2.sql" />
74+
</ItemGroup>
75+
</Project>
76+
```
77+
78+
This prevents the file from being compiled as part of the database model while keeping it visible in the project.
79+
80+
> [!TIP]
6681
> You can validate the pre-deployment and post-deployment scripts after project build, by changing the `.dacpac` file extension to `.zip` and unarchiving the `.zip` to a folder. A single `.sql` file is present for pre-deployment and post-deployment scripts, and should contain the entire Transact-SQL contents of all referenced files in the originating SQL project.
6782
68-
## Add pre- and post-deployment scripts
83+
## Add pre-deployment and post-deployment scripts
6984

7085
::: zone pivot="sq1-visual-studio"
7186

7287
In **Solution Explorer**, right-click the project and select **Add** > **Script**. Select **Pre-Deployment Script** or **Post-Deployment Script**.
7388

74-
The script file is added to the project and opened in the query editor, where you can complete the script. This script will be executed before or after the deployment plan is executed every time the project is deployed.
89+
The script file is added to the project and opened in the query editor, where you can complete the script. This script will be executed before or after the deployment plan is executed, every time the project is deployed.
7590

7691
::: zone-end
7792

7893
::: zone pivot="sq1-visual-studio-sdk"
7994

80-
In **Solution Explorer**, right-click the project node and select **Add**, then **New Item**. The **Add New Item** dialog appears, select **Show All Templates**. and then **Table**. Select **Pre-Deployment Script** or **Post-Deployment Script**.
95+
In **Solution Explorer**, right-click the project node and select **Add** > **New Item**. The **Add New Item** dialog appears, select **Show All Templates** > **Table**, then select **Pre-Deployment Script** or **Post-Deployment Script**.
8196

82-
The script file is added to the project and opened in the query editor, where you can complete the script. This script will be executed before or after the deployment plan is executed every time the project is deployed.
97+
The script file is added to the project and opened in the query editor, where you can complete the script. This script will be executed before or after the deployment plan is executed, every time the project is deployed.
8398

8499
::: zone-end
85100

86101
::: zone pivot="sq1-visual-studio-code"
87102

88-
In the **Database Projects** view of VS Code or Azure Data Studio, right-click the project and select **Add Pre-Deployment Script** or **Add Post-Deployment Script**. Provide a script name without the file extension.
103+
In the **Database Projects** view of Visual Studio Code or Azure Data Studio, right-click the project, and select **Add Pre-Deployment Script** or **Add Post-Deployment Script**. Provide a script name without the file extension.
89104

90-
The script file is added to the project and opened in the query editor, where you can complete the script. This script will be executed before or after the deployment plan is executed every time the project is deployed.
105+
The script file is added to the project and opened in the query editor, where you can complete the script. This script will be executed before or after the deployment plan is executed, every time the project is deployed.
91106

92107
::: zone-end
93108

94109
::: zone pivot="sq1-command-line"
95110

96-
Edit the `.sqlproj` file directly to add pre- or post-deployment scripts. Add a `<PreDeploy>` or `<PostDeploy>` item to the `<ItemGroup>` section of the `.sqlproj` file.
111+
Edit the `.sqlproj` file directly to add pre-deployment or post-deployment scripts. Add a `<PreDeploy>` or `<PostDeploy>` item to the `<ItemGroup>` section of the `.sqlproj` file.
97112

98113
For example, to add the script `scripts\before-script.sql` to our project as a pre-deployment script:
99114

@@ -104,7 +119,7 @@ For example, to add the script `scripts\before-script.sql` to our project as a p
104119
</ItemGroup>
105120
```
106121

107-
This script `scripts\before-script.sql` is executed before the deployment plan is executed every time the project is deployed.
122+
This script `scripts\before-script.sql` is executed before the deployment plan is executed, every time the project is deployed.
108123

109124
::: zone-end
110125

0 commit comments

Comments
 (0)