本文转自:http://www.rad.pasfu.com/index.php?/archives/150-Insert,-Update,-and-Delete-Destination-table-with-SSIS.html

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相关推荐

  1. Attempt to do update or delete on table db1.table1 that is not transactional解决

    在datagrip尝试对hive中的表格进行删除,碰到了这么个问题 Attempt to do update or delete on table db1.table1 that is not tra ...

  2. Attempt to do update or delete on table educator.t_item_info that does not use an AcidOutputFormat o

    Attempt to do update or delete on table educator.t_item_info that does not use an AcidOutputFormat o ...

  3. hive 异常 (Attempt to do update or delete on table terminal that does not use an )

    hive 异常 (Attempt to do update or delete on table terminal that does not use an ) hive > delete fr ...

  4. mybatis(三) XML映射器之select、update、delete、insert标签

    目录 文章目录 3.XML映射器 3.1.select 3.1.1.单表查询 3.1.2.select标签属性列表 3.1.3.parameterType属性 3.1.4.resultType 3.1 ...

  5. 让Hive支持行级insert、update、delete

    首先:欢迎各位学习java和大数据的程序员朋友们加入Java交流学习群: 721506929群里提供免费的架构学习资料,直播讲解,讲师都是有着十几年阿里java开发经验的大牛,欢迎各位前来围观学习. ...

  6. SQLServer之创建INSTEAD OF INSERT,UPDATE,DELETE触发器

    INSTEAD OF触发器工作原理 INSTEAD OF表示并不执行其所定义的操作INSERT,UPDATE ,DELETE,而仅是执行触发器本身,即当对表进行INSERT.UPDATE 或 DELE ...

  7. SQL Server 2008中SQL增强之三:Merge(在一条语句中使用Insert,Update,Delete)

    SQL Server 2008中SQL增强之三:Merge(在一条语句中使用Insert,Update,Delete) SQL Server 2008提供了一个增强的SQL命令Merge,用法参看MS ...

  8. Oracle中5个核心Sql语句的基本构造:Select、Insert、Update、Delete和Merge

    Sql语言提供了很多不同的选择来得到同样的结果集,关键是需要搞清楚在不同的使用场景下哪种构造是最高效的. 1.Select语句 Select语句用来从一个表中,或者其他数据库对象中提取数据. sele ...

  9. 转载:MySQL数据库INSERT、UPDATE、DELETE以及REPLACE语句的用法详解

    转自:http://www.jb51.net/article/39199.htm 本篇文章是对MySQL数据库INSERT.UPDATE.DELETE以及REPLACE语句的用法进行了详细的分析介绍, ...

最新文章

  1. H3 BPM钉钉接入配置
  2. 为什么在Python里推荐使用多进程而不是多线程?(为什么python多线程无法增加CPU使用率?)...
  3. Laravel安装因PHP版本不对的bug
  4. 多晶硅价格已处于阶段性高点
  5. Spring Boot 集成 GRPC
  6. android弹出键盘高度,Android 解决全面屏 软键盘弹出会有高度约50dp的白条。
  7. 全网最详细的idea安装教程
  8. C 语言中的指针和内存泄漏
  9. 爱不意味这“sorry”
  10. 真实的网络赚钱经历:另类推广引流操作CPA!
  11. 三菱伺服驱动器示例_三菱PLC控制伺服电机编程实例
  12. 计算机视觉:步态识别-综述(一)
  13. 转行it学python_转行IT行业为什么要学习Python开发
  14. 戴德金之连续性和无理数的中文翻译
  15. C++的STL中accumulate的用法
  16. macOS根目录上无法写入文件和创建目录的问题
  17. HDU 威威猫系列故事——篮球梦
  18. 潜伏在大厂中“摸鱼”的打工人
  19. 钢铁企业以撮合模式切入B2B平台,汇聚势能实现价值最大化
  20. CSS改变鼠标样式(图片)

热门文章

  1. oracle日常维护(不断更新)
  2. [uboot]在uboot里面添加环境变量使用run来执行
  3. 线上zabbix数据库重建表分区
  4. windows下Emacs的安装与配置
  5. 改进粒子群优化算法(PURPSO)的MATLAB源程序
  6. Windows下开发Perl程序之环境搭建
  7. #周末课堂# 【Linux + JVM + Mysql高级性能优化班】(火热报名中~~~)
  8. 漢城博殺的日子 (四)
  9. redis专题:数据库和redis缓存一致性解决方案
  10. 光大代付支付有问题解决思路