SSIS ->> Null & Null Functions

SSIS不支持值为NULL的变量。每种类型的变量都有自己的默认值。

做了一个测试,用一个Execute SQL Task输出一个NULL值给A变量,然后把A变量传到到另外一个Execute SQL Task插入表,得到的值是0。这里可以证明确实在《Professional Microsoft SQL Server 2012 Integration Services》中Dealing with NULLs一段讲到In SSIS, variables can’t be set to NULL是对的。即便我们有意设置某个值为NULL,结果还是变成了该类型的默认值。

那么在Expression可以用过NULL Functions(如NULL(DT_I4))作为比较对象,比如(@[User::Variable]==NULL(DT_I4))?0:1。当时这里又想到一个问题,如果用来赋值某个变量的表达式本身就可以返回和该类型默认值相等值,这样程序上就会出现Bug。所以在在程序上应该是用另外的值来替代。比如Int8的默认值是0,而为了避免出现错误程序出现错误判断,设计的时候需要制定好其他的值来代替。

Null Functions的作用是返回各种SSIS类型对应的NULL值,因为在T-SQL中的NULL到了SSIS中就必须是分为不同数据类型如NULL(DT_I4)。比如我们希望把源输入中某个INT栏位遇到0就替换NULL(DT_I4),这样进入到目标表中才会是NULL值。

下面是各种变量数据类型和他们对应的默认值

---------------------------------------------------------------------------------------------------------

Null function有一点需要注意的是,SSIS在对非UNICODE数据使用NULL function时,也就是NULL(DT_STR, «length», «code_page»)时,是需要在前面再做一次类型转换的,而不像对UNICODE字符串或者整型类型数据那样简单的用num == 0 ? NULL(DT_I4) : num就行。

在《Professional Microsoft SQL Server 2012 Integration Services》中有这样一段解释:

For some reason, this works differently when you want to save
non-Unicode data with a NULL value. You’d expect the following expression to work, but it doesn’t:
SUBSTRING([MyColumn] , 1, 1)==”A” ?
NULL(DT_STR, 255, 1252) : [MyColumn] (This doesn’t work in SSIS)
The preceding example won’t work because of how SSIS handles NULL values for the non-Unicode
string type as parameters. The only way to fix this is to cast the NULL function as follows:
SUBSTRING([MyColumn] , 1, 1)==”A” ?
(DT_STR, 255, 1252)NULL(DT_STR, 255, 1252) : [MyColumn]
This section should have clarified the common issues you are likely to encounter when dealing with
NULL values, especially as they relate to strings. However, there are still some tricks to learn about
dealing with strings, which we cover next.

原文地址:https://www.cnblogs.com/jenrrychen/p/4492893.html