Pre-Deployment And Post-Deployment Script - SQL Database Project


In our previous article about SQL Server Database Project, we have seen how we can create a database project to handle all our SQL scripts.

Now, in this article, we will discuss how we can create a Pre- and a Post-deployment script - when databases are created or upgraded, data may need to be added, changed, or deleted. Moreover, certain actions may have to occur on the database before and/or after the process completes. Deployment scripts can be used to accomplish this.

Let’s start.

  1. Right Click on the solution and add a new item:

    SQL Server
  1. Select User Scripts and then Pre-Deployment script - Add

    SQL Server
  1. Now, write any script you want to add in pre-deployment.

    SQL Server

  2. Repeat step 2 for Post Deployment script.

    SQL Server

  3. Now, Publish the project – We can see the scripts which were inside the pre-deployment script and post-deployment script at the beginning and the end of the publish script respectively.

    SQL Server


So, we created a deployment script, but you may be wondering how a pre-deployment script could be useful. One situation to consider is a complex migration of data. Using the pre and post-deployment scripts, you could perform some upfront work prior to deployment occurring and then work on that data after the deployment occurs.