在进行ETL开发时,数据类型(Data Type)是最基础的,但也容易被忽略,楼主使用的SQL Server 版本是2012,用此博文记录,经常使用的SSIS数据类型和TSQL数据类型的映射。SSIS的数据类型,是指数据流组件使用的数据类型和变量的数据类型(Data Flow 和 Variable)。html
当数据进入Package的data flow task中时,SSIS 经过数据源组件从数据源抽取(extract)数据,获取元数据类型,并转换成SSIS支持的数据类型,SSIS的数据类型主要分为三类:字符(string),数值(numeric)和日期/时间(date/time),若是源数据相似不能转换成相应的SSIS 数据类型,SSIS Engine就会报错。SSIS的数据类型,以“DT_”开头,是Data Type的简写。sql
一,SSIS 数据流的数据类型和TSQL数据类型的映射express
1,字符类型app
字符类型用于存储字符串,在SQL Server中,使用单引号表示一个字符,可是在SSIS中,使用双引号表示一个字符串。函数
SSIS的字符类型和TSQL的数据类型的对应关系:post
2,数值类型性能
数值类型分为整数和小数,SSIS的整数类型和TSQL数据类型的对应关系:ui
TSQL的小数数值类型分为两类:精确小数(decimal)和近似小数(float),小数也叫实数(real),SSIS的小数类型和TSQL数据类型的对应关系:lua
3,日期时间类型url
SSIS的日期时间类型和TSQL数据类型的对应关系:
SSIS 内置函数:GETDATE() 和 GETUTCDATE() 返回值的数据类型是DT_DBTIMESTAMP,对应TSQL的DateTime,所以,只保留3位毫秒。在Expression Builder中,将时间类型转换成字符串类型,显示的毫秒数有效数值只有3位,末尾补6个0,共9位:
二,SSIS 变量(Variable)的数据类型和TSQL数据类型的映射
SSIS 变量的数据类型,不一样于SSIS的数据类型,但都和SSIS的数据类型相兼容,在进行表达式求值时,SSIS自动将变量的数据类型隐式转换成SSIS的数据类型,而后进行求值。
Variables have a Variant data type and the expression evaluator converts the data type of a variable from a Variant subtype to an Integration Services data type before it evaluates the expression.
1,字符数据类型
字符变量和TSQL数据类型的映射关系:
2,数值类型
数值类型的变量和TSQL数据类型的映射关系:
3,日期/时间类型
日期/时间类型的变量和TSQL数据类型的映射关系:
三,强制类型转换
SSIS在进行表达式求值时,自动将一个数据类型隐式转换成相兼容的另一个数据类型,若是类型不兼容,必须强制类型转换,不然,SSIS报错。对数据进行强制类型转换的格式是:(type) expression,在进行显式类型转换时,尽可能使用窄的数据类型,这样可以提升数据传输的速度;可是,数据转换须要付出必定的代价,所以,必须权衡类型转换和数据传输对性能的影响。
An implicit conversion of a data type occurs when the expression evaluator automatically converts the data from one data type to another. If the data in a column does not require the full width allocated by the source data type, you might want to change the data type of the column. Making each data row as narrow as possible helps optimize performance when transferring data because the narrower each row is, the faster the data is moved from source to destination.
1,将字符串转换成TSQL的日期/时间类型
在SSIS中,字符串常量使用双引号“”,[] 表示可选:
2,转换成字符串
字符串分为双字节字符和单字节字符,对于单字节字符,SSIS使用 DT_STR 表示,在强制类型转换时,必须制定code page和字符长度:
3,数值类型转换
四,数据类型转换的性能
将数据从一个SQL Server 加载到另外一个SQL Server以前,若是须要转换数据类型,建议使用TSQL Conversion,这样,能简化Package的设计,提升转换速度。
五,参数的数据类型
在Execute SQL Task引用变量时,必须在Parameter Mapping Tab中设置参数的Data Type,请参考《Execute SQL Task 参数和变量的映射》
参考文档:
Integration Services (SSIS) Expressions
Integration Services Data Types
SQL Server Integration Services, Data Type Mapping
Performance Comparison between Data Type Conversion Techniques in SSIS 2008
转自 http://www.cnblogs.com/ljhdo/p/5174681.html