Package在执行过程中,不可避免地会发生错误,如果处理错误?简单粗暴的做法,是Package直接停止运行。对于一个成熟的ETL工具,这显然不是唯一的错误处理方法。如果在数据流中出现错误,那么数据流组件可以把错误行输出,这只需要在组件的ErrorOutput中进行简单地配置。跟数据流相比,控制流中包含错误处理程序OnError,对错误事件的处理更加复杂和精细。
用户可以通过MaximumErrorCount属性和OnError事件处理程序来对错误进行控制:
- Package本身、Task 和 Container具有属性MaximumErrorCount,用于控制错误之前组件可以出错的次数 ;
- OnError事件处理程序能够捕获Task或容器中触发的OnError事件,并对错误进行处理。
一,属性MaximumErrorCount用法
属性MaximumErrorCount 指定Task组件在失败之前可以出错的次数,当累积的错误达到属性MaximumErrorCount的值时,Task执行失败。该属性的默认值是1,也就是,只要发生错误,组件就会失败。
1,测试容器的MaximumErrorCount
设置容器的属性MaximumErrorCount=2,其子Task发生一个错误,执行情况如下图所示:
![](https://i-blog.csdnimg.cn/blog_migrate/72449141e8673b8c3ca22061a61dd269.png)
容器执行成功,子Task执行失败,这是由于容器的MaximumErrorCount属性是2,还没有达到失败的阈值,因此容器的执行结果是成功的。在Progress 选项卡中,SSIS 报出Warning信息:
Warning: SSIS Warning Code DTS_W_MAXIMUMERRORCOUNTREACHED. The Execution method succeeded, but the number of errors raised (1) reached the maximum allowed (1); resulting in failure. This occurs when the number of errors reaches the number specified in MaximumErrorCount. Change the MaximumErrorCount or fix the errors.
SSIS Package 最终的执行结果是
![](https://i-blog.csdnimg.cn/blog_migrate/e6d4151726b015c1621954108a7d0aa3.jpeg)
失败的原因是错误会继续向父组件传递,直到传递到Root Level(Package),而Package的MaximumErrorCount=1。
2,测试Package的属性MaximumErrorCount
把Package的属性MaximumErrorCount设置为2,把容器的属性MaximumErrorCount设置1,查看Package的执行结果
![](https://i-blog.csdnimg.cn/blog_migrate/2f8ae4944125c33f9c366534fbb8516a.png)
![](https://i-blog.csdnimg.cn/blog_migrate/707218cbaa0d56755b0838af5ba1f0b5.jpeg)
在Progress Tab中查看执行过程:
[Execute SQL Task2] Error: Executing the query "insert into dbo.test_env
values(1,N'test_error')" failed with the following error: "An explicit value for the identity column in table 'dbo.test_env' can only be specified when a column list is used and IDENTITY_INSERT is ON.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
二,OnError事件的错误处理程序
在一个OnError 事件的处理程序中,如果把系统变量Propagate属性设置为False,那么不需要修改父容器的MaximumErrorCount属性,就能保证在发生错误后,Package可以继续运行。
1,测试系统变量 Propagate
为容器下的Execute SQL Task创建OnError事件处理程序,把OnError的事件处理程序中的系统变量 Propagate设置为False
![](https://i-blog.csdnimg.cn/blog_migrate/bc24668cf75b8acaf5ce45078af62015.jpeg)
查看package的执行结果,虽然Execute SQL Task 2执行失败,但是容器的执行结果是成功的,Package的执行结果也是成功的,如下图所示:
![](https://i-blog.csdnimg.cn/blog_migrate/6a0438ee22a7d6c29c43adf0c6853c0b.png)
![](https://i-blog.csdnimg.cn/blog_migrate/8b8e5aa76f8304672e62e20d75543062.jpeg)
从Progress中查看到的错误消息是:
[Execute SQL Task] Error: Executing the query "insert into dbo.test_envvalues(1,N'test_error')" failed with the following error: "An explicit value for the identity column in table 'dbo.test_env' can only be specified when a column list is used and IDENTITY_INSERT is ON.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.
2,从SSISDB中查看错误
把Package部署到Integration Services Catalog中,查看执行的结果,Satus是Succeeded,Error messages中显示错误的信息。
![](https://i-blog.csdnimg.cn/blog_migrate/8757102332194b6f2f3edef03f4d33cc.jpeg)
四,错误对执行结果和消息的影响
控制流中发生的任何错误,都会被SSIS引擎捕获;不管如何设置Task或Container的属性,只要发生错误,就会产生错误消息。
错误能够向上传递,当错误被OnError事件处理程序捕获时,通过设置系统变量Propagate为False时,可以停止错误向父级别传递。
Package的执行结果收到属性的控制,组件抛出错误,Package仍然可能执行成功,只不过progress中会记录错误消息。