[转]How to handle Failed Rows in a Data Flow
本文转自:http://www.rad.pasfu.com/index.php?/archives/23-How-to-handle-Failed-Rows-in-a-Data-Flow.html
suppose this scenario:
you have a source table and a destination table, you want to transfer rows from source table to destination table, so you can use simple data flow with OLE DB source and OLE DB Destination. but problem hits when destination table has few rows inside, and there is Unique Constraint in ID field of Destination table. in this situation your data flow will fail, because when OLE DB Destination want to insert rows in destination table, it will got Unique Constraint error and it will raise Data Flow error, and this will prevent OLE DB Destination to insert any rows in table, even rows which hasn't equivalent key in destination !
So, a work around needed here to avoid failing package and only move rows which hasn't problem with unique constraint in destination table.
let me explain it with a sample here...
create source table with this script:
CREATE TABLE [dbo].[SourceFailureTable](
[ID] [int] IDENTITY(1,1) NOT NULL,
[name] [varchar](50) NULL,
CONSTRAINT [PK_SourceFailureTable] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
and fill it with this data: 
now create destination table with this structure:
CREATE TABLE [dbo].[DestinationFailureTable]( [ID] [int] NOT NULL, [name] [varchar](50) NULL, CONSTRAINT [PK_DestinationFailureTable] PRIMARY KEY CLUSTERED ( [ID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY]
Note that DestinationFialureTable, has a Primary Key on ID filed, primary key is basically a Unique Constraint. so this constriant will check IDs to be unique , and if an insertion tends to insert not unique value in ID field this will cause Unique Constraint Error.
fill Destination with these three fields:

we want to import rows from SourceFailureTable to DestinationFailureTable. Create simple Data flow task, with an OLE DB Source, connected to SourceFailureTable. and an OLE DB Destination connected to DestinationFailureTable. map the columns too. now every time you run this package you will got error: 
error description: "Violation of PRIMARY KEY constraint 'PK_DestinationFailureTable'. Cannot insert duplicate key in object 'dbo.DestinationFailureTable'."
and none of ten rows in Source table moved to destination table. even rows with IDs like 1 , 2, 4 ,.. which
not exists in the destination table.
So, What we can do? right click on OLE DB Destination, select properties window. find AccessMode in properties, you can see that the AccessMode value is OpenRowSet using FastLoad. this will cause that all records from input insert with a fast load transaction at one time. so this will cause error. Change AccessMode to OpenRowSet . this will try to insert row by row. Note that this is not good option when you insert large amount of records because insertion with fast load has better performance, but in this sample I just want to show you how to handle failed rows in insert. after chaning AccessMode to OpenRowSet , run package again, you will see the error happens again with no luck. but difference is here in the destination table:

in the destination table two new rows inserted , rows with ID 1 and 2. but no more rows. the meaning of this behaviour is that OLE DB Destinaion tried to insert row by row into the DestinationFailureTable, first two rows inserted successfully. but for third row it got unique constraint error again and cause package to fails. and failing the package stops other rows transformation.
So you should handle rows which cause Error. this is the time when Failure Error Output comes to help. first delete rows with ID 1 and 2 from DestinationFailureTable manually. and then add another table named FailedRows with this structure:
CREATE TABLE [dbo].[FailedRows](
[ID] [int] NULL,
[name] [varchar](50) NULL
) we want to move rows which cause error to this table.
now add another OLE DB Destination in data flow , right after DestinationFailureTable . and connect RED ARROW to it. when you connect this arrow, a Configure Error Output window will appear. look at this screenshot:

there are three options for Error property in this window:
Ignore Failure will ignore any errors during insertion of records to destination Redirect Row will redirect any rows which cause error during insertion to destination Fail Component will cause to fail component when insertion hits any error
by default this option set to Fail Component. so when you got an error during insertion the whole OLE DB Destination will fail. Set the Error as Redirect row. this will cause every rows which cause error ( means BAD rows ) to redirect to new OLE DB Destination. now hit ok. and double click on new OLE DB Destination let's name it FailedRows. connect this to failed rows. and map the columns like below:

Note that there are two new columns in mapping page, ErrorCode and ErrorColumn. these are auto generated rows by Failure Error Output and will show code and column number of error. we don't need them in this sample. so just map ID and name.
now run the package. you will see that there are 10 rows transfered from SourceFailureTable to DestinationFailureTable, but there are only 3 rows transfered to FailedRows. this means that there are 3 rows exists in DestiantionFailureTable which prevent equivalent values in SourceFailureTable to be inserted . so these rows transfered to FailedRows Table. this is full schema of package:

and look at results in DestinationFailureTable:

there are 7 new rows in this table.
and this is results in FailedRows Table:

there are 3 rows which cause unique constraint failure transfered to this table.
This was a sample of how to use failure error output in SSIS. hope to solve your issues in this area.
[转]How to handle Failed Rows in a Data Flow的更多相关文章
- 【err】开启Persistence-M模式-Check failed: err == CUBLAS_STATUS_SUCCESS (1 vs. 0) : Create cublas handle failed
前言 安装好CUDA.CUDNN.NVIDIA driver之后,使用mxnet框架的时候出现该错误,本文记录该问题的解决方法. 环境 ubuntu 16.04 MxNet Cuda9.0 Nvidi ...
- SMTP ERROR: Password command failed: 535 Incorrect authentication data
在处理一个使用PHPMailer来发送电邮,我在本地使用我的163邮箱来做测试发送电邮,能够成功的发送电邮:当上传到正式平台时,出现了,类似这样的错误信息 SMTP ERROR: Password c ...
- How to use dt.Rows.Cast<System.Data.DataRow>().Take(n)
参考文章:http://stackoverflow.com/questions/2787458/how-to-select-top-n-rows-from-a-datatable-dataview-i ...
- MYSQL安装报错 -- 出现Failed to find valid data directory.
运行环境:windows10数据库版本:mysql.8.0.12安装方式:rpm包直接安装 问题描述:mysql初始化的时候找不到对应的数据库存储目录 报错代码: 2018-10-13T03:29:2 ...
- PHP+mysql系统报错:PHP message: PHP Warning: Unknown: Failed to write session data (files)
PHP+mysql系统报错:PHP message: PHP Warning: Unknown: Failed to write session data (files) 故障现象,后台页面点击没有 ...
- IIS "rewrite.dll failed to load. The data is the error." 错误解决方法
在Windows 10 build 17133.73上部署一个较老版本的ASP.NET 2.0程序,访问时候出现: Service Unavailable HTTP Error 503. The se ...
- failed to load response data
当需要根据后台传回地址跳转页面时 即使使用preserve log 可以查看上一个页面获取地址请求,但是此时请求返回值为failed to load response data 当关闭页面跳转可以查看 ...
- [转载]Failed to read session data On PHP 7.1
从php5.6升级php7.1,报错 Warning: session_start(): Failed to read session data: user (path: ) Warning: ses ...
- 解决gpg failed to sign the data fatal: failed to write commit object解决方案
今天有位新同事在comit代码的时候一直报这个错误: gpg failed to sign the data fatal: failed to write commit object. 看到网上说gp ...
随机推荐
- Linux网络编程:一个简单的正向代理服务器的实现
Linux是一个可靠性非常高的操作系统,但是所有用过Linux的朋友都会感觉到, Linux和Windows这样的"傻瓜"操作系统(这里丝毫没有贬低Windows的意思,相反这应该 ...
- 【转载】PDB命令行调试Python代码
转载自这里. (博主按:PDB调试python代码和用GDB调试c++代码很类似) 你有多少次陷入不得不更改别人代码的境地?如果你是一个开发团队的一员,那么你遇到上述境地的次数比你想要的还要多.然而, ...
- .NET 二进制序列化器,SOAP序列化器,XML序列化器
这里就不说JSON序列化了,只介绍三种:二进制序列化器,SOAP序列化器,XML序列化器 直接上代码: /// <summary> /// 二进制序列化器. /// 最节省流量,压缩程度最 ...
- Windows服务器学习篇:服务器连接与退出
此文是我早期在公司内部发布的一篇给予新入职程序员基础技术培训的文章,非常基础简单,现拿出来给大家分享.当然,已工作人士可直接忽略... 一.Windows服务器连接 1. 在桌面菜单中的“运行”里,输 ...
- Centos7使用squid实现正向代理
正向代理:代理服务器帮助客户端(浏览器)实现互联网的访问 (1)代理服务器配置 1.安装squid yum install squid -y 2.编辑squid配置文件 #vim /etc/squid ...
- ffmpeg测试程序
ffmpeg在编译安装后在源码目录运行make fate可以编译并运行测试程序.如果提示找不到ffmpeg的库用LD_LIBRARY_PATH指定一下库安装的目录.
- 使用for循环打印9×9乘法表
请使用for循环,倒序打印9×9乘法表. 打印结果如下图所示: 使用for循环打印9×9乘法表 #include <stdio.h> int main() { int i, j, resu ...
- Laravel 下配置 Redis 让缓存、Session 各自使用不同的 Redis 数据库
为什么要这样做? 默认情况下,Redis 服务会提供 16 个数据库,Laravel 使用数据库 0 (请见 Redis 文档)作为缓存和 Session 的存储. 在使用的过程中觉得这个默认的设置挺 ...
- 区间DP(区间最优解)题目讲解总结
1:给出一个括号字符串,问这个字符串中符合规则的最长子串的长度. [分析]区间DP要覆盖整个区间,那么要求所有情况的并集. 先想出状态方程: dp[i][j]:i ~ j区间内最大匹配数目 输出:dp ...
- nyoj(表达式求值)
描述 ACM队的mdd想做一个计算器,但是,他要做的不仅仅是一计算一个A+B的计算器,他想实现随便输入一个表达式都能求出它的值的计算器,现在请你帮助他来实现这个计算器吧. 比如输入:"1+2 ...