Introduction
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.
- Right Click on the solution and add a new item:
- Select User Scripts and then Pre-Deployment script - Add
- Now, write any script you want to add in pre-deployment.
- Repeat step 2 for Post Deployment script.
- 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.
Conclusion
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.