Learn more about SQL Server tools

mssqltips logo
 

Tutorials          DBA          Dev          BI          Career          Categories          Webcasts          Whitepapers          Today's Tip          Join

Tutorials      DBA      Dev      BI      Categories      Webcasts

DBA    Dev    BI    Categories

 

SSIS Package for Automated Code Deployment of SQL Server Database Changes


By:   |   Last Updated: 2017-01-02   |   Comments   |   Related Tips: More > Database Administration

Problem

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.

Solution

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 Deployment Overview

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 Variables

SSIS Package Connection Mangers

  • Folder - SQL schema deployment folder
  • OLEDB Connection - OLEDB connection to target SQL Server
SSIS Package Connection Mangers

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".

SSIS Foreach Loop Configuration

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.

SQL Task in SSIS for Deployment

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”.

Successful File System Task in SSIS

After successful execution, the file has been renamed with the suffix “Deployed.DDMMYYYYHHMMSS”.

After successful execution, the file has been renamed with the suffix Deployed.DDMMYYYYHHMMSS
Sucessful SSIS Deployment

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.

Failed File System Task in SSIS

As the second script has failed it will be renamed with the suffix “.Error.DDMMYYYYHHMMSS”.

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.

Summary

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.

Next Steps
  • Read more about Building and Deploying a Database here
  • Read about Deploy changes to new or existing databases here


Last Updated: 2017-01-02


next webcast button


next tip button



About the author
MSSQLTips author Nat Sundar Nat Sundar is working as an independent SQL BI consultant in the UK with a Bachelors Degree in Engineering.

View all my tips
Related Resources




Post a comment or let the author know this tip helped.

All comments are reviewed, so stay on subject or we may delete your comment. Note: your email address is not published. Required fields are marked with an asterisk (*).

*Name    *Email    Email me updates 


Signup for our newsletter
 I agree by submitting my data to receive communications, account updates and/or special offers about SQL Server from MSSQLTips and/or its Sponsors. I have read the privacy statement and understand I may unsubscribe at any time.



    



Learn more about SQL Server tools