[转]Insert, Update, and Delete Destination table with SSIS

Previously I’ve wrote about design and implementation an UPSERT with SSIS. UPSERT is about Update existing records, and Insert new records. Today I want to extend this to cover DELETED records as well. So method used in this post can be used to find INSERTED / UPDATED / DELETED records from the source table and apply those changes into the destination table.
In this example I used Merge Join Transformation, Conditional Split, and OLE DB Command transform to implement the solution. First we apply a full outer join on source and destination table on key column(s) with Merge Join transformation. Then we use a conditional split to find out the change type (removed, new, or existing records). Existing records will require another processing to find out is there any changes happened or not? We use another conditional split to compare value of equivalent columns in source and destination.
Source table used in this example is Department table from AdventureWorks2012 sample database which you can download online for free.
Solution:
1- Create an OLE DB Source for source table, use select command below to select data:
select *
from dbo.Department
order by DepartmentID
Note to the ORDER BY Clause in this statement. That part is required because Merge Join transform require sorted sources as input. Name this component as Source Table
2- Create another OLE DB Source for destination table. In this example source and destination has same table name but are in different databases. So we use same script as step 1 for this one as well. Name this component as Destination Table.
3- Right click on OLE DB Source, choose Show Advanced Editor. In the Advanced Editor window go to Input and Output Properties tab. Select the OLE DB Source Output, and change the IsSorted Property to true.
4- Expand OLE DB Source output, and then under Output Columns select DepartmentID. Then change the SortKeyPosition to 1.
5- Apply steps 3 and 4 for both OLE DB Sources (Source Table and Destination Table)
6- Drag and drop a Merge Join transformation, connect two OLE DB Sources to this. Set Source Table as left and Destination Table as right input of this transformation.
7- Go to Merge Join transformation editor, DepartmentID will be used as joining column (selected based on sort properties of previous components). Note that if you don’t sort input columns of the merge join transformation then you cannot get into the editor of this transformation and you face the error regarding sorting of inputs.
Select all columns from Source and Destination tables in the merge join transform, and rename them as picture below shows (add Source or Destination prefix to each column)
8- Add a Conditional Split transformation and write two expressions below to find out new records, and removed records. Also rename default output as existing records and screenshot below shows
Expressions used in this sample are very easy and simply find record changes. For example expression below:
!ISNULL(SourceDepartmentID) && ISNULL(DestinationDepartmentID)
Used to find new records. And literally means records that has SourceDepartmentID but not DestinationDepartmentID.
And this script used to find deleted records:
ISNULL(SourceDepartmentID) && !ISNULL(DestinationDepartmentID)
9- Add an OLE DB Destination and connect NEW RECORDS output to it. Set configuration for destination table and use columns with Source prefix in the column mapping of the OLE DB destination. This destination component will insert new records into the destination table.
10- Add an OLE DB Command and connect Removed RECORDS output to it. Create a connection to destination database, and write script below to delete records by input department ID:
delete from dbo.department where DepartmentID=?
In the column mappings, map DestinationDepartmentID to the parameter of statement.
11- Add another Conditional Split and connect Existing Records output to it. We use this component to find only records that had a change in one of the values. So we compare equivalent source and destination columns to find non-match data.
This is the expression used to find match data in screenshot below:
(SourceName == DestinationName) && (SourceGroupName == DestinationGroupName) && (SourceModifiedDate == DestinaitonModifiedDate)
12- Create a stored procedure in destination database to update the Department table.
CREATE PROCEDURE dbo.UpdateDepartment
@DepartmentID smallint
,@Name nvarchar(50)
,@GroupName nvarchar(50)
,@ModifiedDate datetime
AS
BEGIN
SET NOCOUNT ON;
UPDATE [dbo].[Department]
SET
[Name] = @Name
,[GroupName] = @GroupName
,[ModifiedDate] = @ModifiedDate
WHERE [DepartmentID] = @DepartmentID
END
13- Add another OLE DB Command and use non match output as the input data stream to it. Connect it to destination database, and write below statement in Component Properties tab’s SQLCommand property.
exec dbo.UpdateDepartment ?,?,?,?
14- Map input columns (with source prefixes) to parameters in the stored procedure as screenshot below shows
15- Run the package and you will see changes will be applied to destination table.
Testing the solution:
Here is data rows from source table
And data rows from destination table
Yellow records are new records
Pink records are updated records
Green record is deleted record (in destination table)
After running the package you will see records will be redirected to data path as implemented:
And destination table will pick changes:
[转]Insert, Update, and Delete Destination table with SSIS的更多相关文章
- Java-MyBatis-MyBatis3-XML映射文件:insert, update 和 delete
ylbtech-Java-MyBatis-MyBatis3-XML映射文件:insert, update 和 delete 1.返回顶部 1. insert, update 和 delete 数据变更 ...
- 关于MyBatis mapper的insert, update, delete返回值
这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...
- mybatis select/insert/update/delete
这里做了比较清晰的解释: http://mybatis.github.io/mybatis-3/java-api.html SqlSession As mentioned above, the Sql ...
- [转] hive0.14-insert、update、delete操作测试
FROM : http://blog.csdn.net/hi_box/article/details/40820341 首先用最普通的建表语句建一个表: hive>create table te ...
- Hive支持行级update、delete时遇到的问题
Hive从0.14版本开始支持事务和行级更新,但缺省是不支持的,需要一些附加的配置.要想支持行级insert.update.delete,需要配置Hive支持事务.(行级的insert好像不配置也能运 ...
- MySQL数据库INSERT、UPDATE、DELETE以及REPLACE语句的用法详解
本篇文章是对MySQL数据库INSERT.UPDATE.DELETE以及REPLACE语句的用法进行了详细的分析介绍,需要的朋友参考下 MySQL数据库insert和update语句引:用于操作数 ...
- 利用insert,update和delete注入获取数据
0x00 简介 利用SQL注入获取数据库数据,利用的方法可以大致分为联合查询.报错.布尔盲注以及延时注入,通常这些方法都是基于select查询语句中的SQL注射点来实现的.那么,当我们发现了一个基于i ...
- Mysql 下 Insert、Update、Delete、Order By、Group By注入
Insert: 语法:INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....) 报错注入: insert into test(id,name,p ...
- [Hive - LanguageManual] DML: Load, Insert, Update, Delete
LanguageManual DML Hive Data Manipulation Language Hive Data Manipulation Language Loading files int ...
随机推荐
- 五:ZooKeeper的集群命令客户端的链接和命令操作的使用
一:zookeeper客户端链接[1]进入zookeeper的安装目录的bin目录下 # cd /opt/zookeeper/bin[2]敲击链接客户端的命令(zkCli.sh) ...
- hdu 1203(01背包)被初始化坑惨了
I NEED A OFFER! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)T ...
- 【剑指offer】面试题 49. 丑数
面试题 49. 丑数 题目描述 题目:把只包含因子2.3和5的数称作丑数(Ugly Number).例如6.8都是丑数,但14不是,因为它包含因子7. 习惯上我们把1当做是第一个丑数.求按从小到大的顺 ...
- php类的定义
<?php /** * Created by PhpStorm. */ class People { //支持带参数 //类的构造方法 /** * Man constructor. * @par ...
- Ubuntu Nginx
如果是阿云的ECS服务器,默认是已经安装了Apache服务器的,但一般都用不到,可以选择将它卸载 sudo service apache2 stop update-rc.d -f apache2 re ...
- Eclipse generate javadoc
注:若遇到导出文档乱码,则点击上图的[next]按钮,在vm options的输入框输入 -J-Xmx180m —- 设置内存大小 (若遇到内存溢出时) -encoding utf-8 ...
- python excellent code link
1. Howdoi Howdoi is a code search tool, written in Python. 2. Flask Flask is a microframework for Py ...
- 可持久化01Trie树【p4735(bzoj3261)】最大异或和
Description 给定一个非负整数序列\(\{a\}\),初始长度为\(N\). 有\(M\)个操作,有以下两种操作类型: A x:添加操作,表示在序列末尾添加一个数\(x\),序列的长度\(N ...
- python3.6下安装结巴分词需要注意的地方
近期,在安装结巴分词的时候遇到一些问题,纠结了好一阵,跟大家分享下,希望能有所帮助.先说下安装环境: windows7, 64位系统 python3.6,python3.5在结巴分词的官方github ...
- oracle substr
SUBSTR( string, start_position [, length ] ) Parameters or Arguments string The source string. start ...