By:Nat Sundar || 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 Package for Automated Code Deployment of SQL Server Database Changes
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 for Automated Code Deployment of SQL Server Database Changes
SSIS Package Connection Mangers Folder - SQL schema deployment folder OLEDB Connection - OLEDB connection to target SQL Server
SSIS Package for Automated Code Deployment of SQL Server Database Changes
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 Package for Automated Code Deployment of SQL Server Database Changes
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.


SSIS Package for Automated Code Deployment of SQL Server Database Changes
Successful File System Task in SSIS

Now lets execute the package to deploy the SQL script. During the first iteration, the forloop has identified the file dbo.GetListOfCurrencies.sql.


SSIS Package for Automated Code Deployment of SQL Server Database Changes

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


SSIS Package for Automated Code Deployment of SQL Server Database Changes
SSIS Package for Automated Code Deployment of SQL Server Database Changes
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.


SSIS Package for Automated Code Deployment of SQL Server Database Changes

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


SSIS Package for Automated Code Deployment of SQL Server Database Changes
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 Databasehere Read about Deploy changes to new or existing databases here Last Update: 1/2/2017

本文数据库(mssql)相关术语:熊片数据库 mssql数据库 oracle数据库 pubmed数据库 access数据库 万方数据库

主题: SQLSQL ServerPowerShellC#.Net
分页:12
转载请注明
本文标题:SSIS Package for Automated Code Deployment of SQL Server Database Changes
本站链接:http://www.codesec.net/view/520044.html
分享请点击:


1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责;
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性,不作出任何保证或承若;
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。
登录后可拥有收藏文章、关注作者等权限...
技术大类 技术大类 | 数据库(mssql) | 评论(0) | 阅读(18)