[转]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 ...
随机推荐
- WPS2019体验
不久之前WPS2019发布了, 说实话, 做的真的不错. 没找到2016版本多得吓人的广告, 没有那糟糕的页面设计, 没有那卡顿的体验. 而且不同的程序(文字, 演示)做成了类似标签页的形式, 体验比 ...
- Linux下undefined reference to ‘pthread_create’问题解决 zz
接触了Linux系统编程中的线程编程模块,可gcc sample.c(习惯把书上的sample代码写进sample.c文件中)出现“undefined reference to ‘pthread_cr ...
- [BZOJ4456] [Zjoi2016]旅行者 分治+最短路
4456: [Zjoi2016]旅行者 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 777 Solved: 439[Submit][Status] ...
- shadownsocks SSR 账号密码注册 可1元体验一天
shadownsocks SSR 账号密码注册 可1元体验一天 注册地址 https://www.cup123.club/register?aff=809
- maven创建springboot项目
1.new Project 2.选择spring Initializr 3.选择next,可以自定义group.artifact,type里可以选择maven也可以选择gradle 4.选择sprin ...
- 五十三 网络编程 TCP/IP简介
虽然大家现在对互联网很熟悉,但是计算机网络的出现比互联网要早很多. 计算机为了联网,就必须规定通信协议,早期的计算机网络,都是由各厂商自己规定一套协议,IBM.Apple和Microsoft都有各自的 ...
- vue-music 关于Search(搜索页面)-- 搜索结果优化
搜索结果 列表点击跳转到相应的歌手详情页或者 歌曲页面,通过子路由跳转,和singer 组件一样 在suggest.vue 组件判断如果点击的是歌手,则new 一个歌手对象,通过这个对象的id 属性值 ...
- python之sqlite3使用详解
Python SQLITE数据库是一款非常小巧的嵌入式开源数据库软件,也就是说没有独立的维护进程,所有的维护都来自于程序本身.它使用一个文件存储整个数据库,操 作十分方便.它的最大优点是使用方便,功能 ...
- hihoCoder #1871 : Heshen's Account Book-字符串暴力模拟 自闭(getline()函数) (ACM-ICPC Asia Beijing Regional Contest 2018 Reproduction B) 2018 ICPC 北京区域赛现场赛B
P2 : Heshen's Account Book Time Limit:1000ms Case Time Limit:1000ms Memory Limit:512MB Description H ...
- 妙用next数组打表求最小循环节len
https://www.cnblogs.com/njczy2010/p/3930688.html https://blog.csdn.net/dominating413421391/article/d ...