[转]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 ...
随机推荐
- python初学-元组、集合
元组: 元组基本和列表一样,区别是 元组的值一旦创建 就不能改变了 tup1=(1,2,3,4,5) print(tup1[2]) ---------------------------------- ...
- MiCode 40: 找小“3”
题目链接 这道题真的是zjb恶心, 看其起来像是个数位dp, 然而我并不会数位dp.然后就xjb乱写了个雷类似于动态规划的玩意, 然后调出了\(9\times 9 = 81\)种Bug, 终于过了. ...
- django使用用户名或手机号码登录
django有自己的认证系统,会自动根据用户名和密码进行验证.如果需要使用用户名或手机登录的话,需要重写django的认证后台,并且设置到配置文件中. 重写django的认证后台 class User ...
- 熟悉并了解uml的使用(一)
本资料对UML各种模型图的构成和功能进行说明,通过本资料的学习达到可以读懂UML模型图的目的.本资料不涉及模型图作成的要点等相关知识. UML简介 UML (Unified Modeling Lang ...
- LoadRunner读取文件并验证
checkprocess() { char command[1024]; int i, total = 0; char buffer[12], ch; char *filen ...
- centos7yum安装tomcat
执行安装命令 [root@localhost ~]# yum -y install tomcat 查询tomcat是否安装成功 [root@localhost ~]# rpm -q tomcat to ...
- Ionic-wechat项目边开发边学(一):环境搭建和创建一个项目
之前学AngularJS,教程过了一遍觉得很简单,但真正写几个Demo就错误百出,一个小小的功能要折腾很久.所以这次学Ionic,准备以开发一个项目为切入点去学,那么问题来了,开发什么项目呢? 纠结了 ...
- java File类 打印目录树状结构(递归)
import java.io.File; /** * 递归遍历 * */ public class FieTree { public static void main(String[] args) { ...
- 定位所用的class
方案 为解决类冲突,我们可以使用下述的方案定位一个class所在的位置 ClassName. package cn.j2se.junit.classpath; import static org.ju ...
- 查看所有shell类型
[xf@xuexi ~]$ cat /etc/shells /bin/sh /bin/bash /sbin/nologin /usr/bin/sh /usr/bin/bash /usr/sbin/no ...