SSIS Package for Automated Code Deployment of SQL Server Database Changes
All SQL Server DBAs and Developers need a way to deploy there T-SQL code. There are several ways that this can be done. In this tip we look at an approach that uses SQL Server Integration Services (SSIS) as the deployment tool.
There are various tools available in the market to deploy SQL schema changes to target a SQL Server environment. In addition, organizations develop their own applications to deploy T-SQL changes. The applications can make use of PowerShell scripts, command line scripts, C#/VB.Net, etc. In this tip, we will look at how to use SSIS.
Benefits of deployment automation
Deployment automation benefits can be summarized as:
- Human errors can be eliminated over a period of time
- No need to have a special expert for deployment
- Setting up a new environment is simple and easy
- Deployments can be done more frequently
Using SSIS for deployment automation
In this tip, we will look into one aspect of deploying SQL schema changes using a SQL Server Integration Services package. We assume all the schema changes are available as a .sql file in a folder. The SSIS package will loop thru and deploy the schema against the target server. If the deployment is successful, the schema file will be renamed with the date and time stamp. If there is a failure or issue, the file will be renamed with an error tag.
The below highlights the SSIS package control flow.
SSIS Package Variables
These are the variables that have been defined in the package:
- FileName - The for each loop container will iterate thru the available .sql files and return the fileName for further processing
- CurrentDateTime - Current date and time in (string) ddmmyyyyhhmmss format
- DeployedlFileName - Highlights the successfully processed file name
- ErrorFileName - Highlights the failed file name
SSIS Package Connection Mangers
- Folder - SQL schema deployment folder
- OLEDB Connection - OLEDB connection to target SQL Server
Foreach Loop Configuration
The Foreach Loop has been configured to look for a file in the folder that has the .sql files. Once a file has been identified, it will assign the value of file name to the variable "FileName".
SQL Task in SSIS for Deployment
The SQL task has been configured to read the SQL script from the "FileName" variable. The SQL script will be executed against the OLEDB connection to deploy the schema change.
Successful File System Task in SSIS
Now let’s execute the package to deploy the SQL script. During the first iteration, the forloop has identified the file “dbo.GetListOfCurrencies.sql”.
After successful execution, the file has been renamed with the suffix “Deployed.DDMMYYYYHHMMSS”.
Failed File System Task in SSIS
If there is a failure, then the file will be renamed with an error tag. For testing purposes, I introduced a small bug in the script “dimCurrency.sql”. The first script will be deployed successfully, however the second script "dimCurrency.sql" will fail and the package will fail.
The below picture confirms that the package has failed during the second iteration.
As the second script has failed it will be renamed with the suffix “.Error.DDMMYYYYHHMMSS”.
Thoughts for further improvement
- This solution can be further enhanced by adding a notification (send mail task) to the interested parties.
- The Foreach Loop can be configured to loop thru the folders and sub folders to deploy the SQL changes.
- This could be enhanced to support dependencies between the SQL objects (tables, stored procedures and functions), so they are deployed in the correct order.
- Add an additional variable for the Path where the .sql files reside.
This approach is simple and can be used to further develop a custom deployment utility. I recommend you try and evaluate multiple options before coming to a conclusion on what approach to take.
- Read more about Building and Deploying a Database here
- Read about Deploy changes to new or existing databases here
About the author
This author pledges the content of this article is based on professional experience and not AI generated.
View all my tips