在将NULL值从文件传输到表时,无法重现StackOverflow文章的成功

时间:2013-01-15 19:14:19

标签: sql-server-2008 ssis

我遇到了一个问题,试图重复this StackOverflow文章的成功,将CSV文件中的NULL值转移到允许NULL的表列。

鉴于此CSV文件包含文本分隔符=双引号和列分隔符字符=逗号。请注意,有两个记录,分别是结尾的第3和第4列,“CELL_TXT”和“CELL_VAL”。第一列“CELL_TXT”包含文本值。如果此文本值可以计算为数字,则CELL_VAL将包含此数值,否则为NULL。

"FLDR","FILE_NM","TAB_IDX","TAB_NM","TAB_PART","LABEL_DESGTR","LABEL_TXT","SECT_NM","SEG_NM","COLMN_TXT","ROW_IDX","COLMN_IDX","COLMN_LETTR","CELL_TXT","CELL_VAL","LAST_OPER_ID","LAST_TIMESTMP"
"C:\corp_mlr_rebates\Processing\HHS\CGLIC","MLR_Template_CGLIC_Grand_Total.xls","1","Pt 1 and 2","Part 1","1.","Premium","Health Insurance Coverage","Individual","Total as of 12/31/11","19","6","F","2",2,"HHSSWEEP","1/15/2013 1:40:20 PM"
"C:\corp_mlr_rebates\Processing\HHS\CGLIC","MLR_Template_CGLIC_Grand_Total.xls","1","Pt 1 and 2","Part 1","1.","Premium","Health Insurance Coverage","Individual","3/31/12","19","7","G","",,"HHSSWEEP","1/15/2013 1:40:20 PM"

请注意,对于第二行,CELL_TEXT为空,“”,因此CELL_VAL在CSV文件中存储为NULL,由前一列分隔符后面的连续逗号列分隔符表示。

我正在尝试将此文件导入到允许CELL_VAL表中为NULL的表中。这是完整的表,尽管我们对CELL_TXT感兴趣,特别是CELL_VAL列。

CREATE TABLE [dbo].[HHS_GRD_STG](
    [FLDR] [varchar](255) NOT NULL,
    [FILE_NM] [varchar](80) NOT NULL,
    [TAB_IDX] [int] NOT NULL,
    [TAB_NM] [varchar](80) NOT NULL,
    [TAB_PART] [varchar](80) NOT NULL,
    [LABEL_DESGTR] [varchar](80) NOT NULL,
    [LABEL_TXT] [varchar](255) NOT NULL,
    [SECT_NM] [varchar](80) NOT NULL,
    [SEG_NM] [varchar](80) NOT NULL,
    [COLMN_TXT] [varchar](80) NOT NULL,
    [ROW_IDX] [int] NOT NULL,
    [COLMN_IDX] [int] NOT NULL,
    [COLMN_LETTR] [char](2) NOT NULL,
    [CELL_TXT] [varchar](255) NOT NULL,
    [CELL_VAL] [decimal](14, 4) NULL,
    [LAST_OPER_ID] [char](8) NOT NULL,
    [LAST_TIMESTMP] [datetime] NOT NULL
) ON [PRIMARY]

在SSIS中,我将输入文件对象定义为逗号Delimeted文件,其中“文本限定符和{CR} {LF}为行Delimeter。

在数据流任务中,我在FIle Source对象中将“保留空值”设置为true。在目标Table对象中,我还设置了“Keep Nulls”属性,但是当我运行包时,我收到以下错误。

Error: 0xC0202009 at Load HHS Child tables, HHS_GRD_STG Table [579]: SSIS Error Code DTS_E_OLEDBERROR.  An OLE DB error has occurred. Error code: 0x80004005.
An OLE DB record is available.  Source: "Microsoft SQL Server Native Client 10.0"  Hresult: 0x80004005  Description: "Unspecified error".
Error: 0xC020901C at Load HHS Child tables, HHS_GRD_STG Table [579]: There was an error with input column "CELL_TXT" (659) on input "OLE DB Destination Input" (592). The column status returned was: "The value violated the integrity constraints for the column.".
Error: 0xC0209029 at Load HHS Child tables, HHS_GRD_STG Table [579]: SSIS Error Code DTS_E_INDUCEDTRANSFORMFAILUREONERROR.  The "input "OLE DB Destination Input" (592)" failed because error code 0xC020907D occurred, and the error row disposition on "input "OLE DB Destination Input" (592)" specifies failure on error. An error occurred on the specified object of the specified component.  There may be error messages posted before this with more information about the failure.
Error: 0xC0047022 at Load HHS Child tables, SSIS.Pipeline: SSIS Error Code DTS_E_PROCESSINPUTFAILED.  The ProcessInput method on component "HHS_GRD_STG Table" (579) failed with error code 0xC0209029 while processing input "OLE DB Destination Input" (592). The identified component returned an error from the ProcessInput method. The error is specific to the component, but the error is fatal and will cause the Data Flow task to stop running.  There may be error messages posted before this with more information about the failure.

我注意到文件的第一条记录已成功传输到dest表。这是NON NULL值的记录。

由于Destination表允许CELL_VAL列的空值,为什么此任务失败?

[CELL_VAL] [decimal](14, 4) NULL,

跟进问题:

William正确地指出,当列不允许NULL时,由于另一列包含空值而导致问题。

这是因为我的包似乎没有区分空字符串值

,"",

和NULL值

,,

我指定我的文件为此目的使用双引号作为文本分隔符,但它似乎没有帮助。因此,由于设置为“保留NULLS”似乎是一个文件级属性,我被迫将几列的DDL更改为“允许空”,而我真正想要的是将其他列设置为当我有一个由两个连续文本分隔符表示的值时,为零长度字符串。

是否可以让SSIS区分NULL和零长度字符串值?

1 个答案:

答案 0 :(得分:2)

在我看来,您显示的错误与CELL_VAL列无关,而与CELL_TXT有关。

There was an error with input column "CELL_TXT" (659) on input "OLE DB Destination
Input" (592). The column status returned was: "The value violated the integrity 
constraints for the column.".

这似乎表明在CELL_TXT列上存在外键关系或其他约束,因此不允许使用空值。

它与CELL_VAL中的空值无关。