sql-server - SSIS 参差不齐的文件无法识别 CRLF

标签 sql-server csv ssis etl flat-file

在 SSIS 中,我尝试从平面文件加载数据。 平面文件具有固定宽度的列,但有些列不在一行中(一列可以有一个 CRLF,它必须是一个新行)像这样

a    b      c
the  first  rowok<CRLF>
iu   jjrjdd<CRLF>
this is a   newline<CRLF>

我如何才能在我的输出中拥有完全相同的行数和准确的数据?

我设置了一个平面文件连接,类型不规则。

在这个示例中,第 1 行被正确检索,但对于第 2 行,它无法识别 CRLF,并将第 3 行全部放入 b 列。

最佳答案

解决方法

  1. 在平面文件连接管理器中将整行读取为一列(仅添加一列类型为 DT_STR 且长度为 4000 的列)

enter image description here

  1. 然后在数据流任务中添加一个脚本组件
  2. 添加 DT_STR 类型的三个输出列 (a,b,c)

enter image description here

  1. 编写一个脚本来拆分每一行并将值放入列中(如果缺少一个值则为空)(我使用了 vb.net)

制表符分隔列

    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)


        If Not Row.Column0_IsNull AndAlso
                Not String.IsNullOrEmpty(Row.Column0.Trim) Then

            Dim str() As String = Row.Column0.Split(CChar(vbTab))


            If str.Length >= 3 Then

                Row.a = str(0)
                Row.b = str(1)
                Row.c = str(2)

            ElseIf str.Length = 2 Then

                Row.a = str(0)
                Row.b = str(1)
                Row.c_IsNull = True

            ElseIf str.Length = 1 Then


                Row.a = str(0)
                Row.b_IsNull = True
                Row.c_IsNull = True



            Else

                Row.a_IsNull = True
                Row.b_IsNull = True
                Row.c_IsNull = True


            End If




        Else

            Row.a_IsNull = True
            Row.b_IsNull = True
            Row.c_IsNull = True

        End If


    End Sub

固定宽度的列

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)


    If Not Row.Column0_IsNull AndAlso
                Not String.IsNullOrEmpty(Row.Column0.Trim) Then

        'Assuming that
        'Col a => 0-5
        'Col b => 5-15
        'Col c => 15-

        Dim intlength As Integer = Row.Column0.Length



        If intlength <= 5 Then

            Row.a = Row.Column0
            Row.b_IsNull = True
            Row.c_IsNull = True

        ElseIf intlength > 5 AndAlso intlength <= 15 Then


            Row.a = Row.Column0.Substring(0, 5)
            Row.b = Row.Column0.Substring(5, 10)
            Row.c_IsNull = True

        ElseIf intlength > 15 Then

            Row.a = Row.Column0.Substring(0, 5)
            Row.b = Row.Column0.Substring(5, 10)
            Row.c = Row.Column0.Substring(15)

        End If



    Else

        Row.a_IsNull = True
        Row.b_IsNull = True
        Row.c_IsNull = True

    End If


End Sub

您还可以使用派生列转换来实现此目的

关于sql-server - SSIS 参差不齐的文件无法识别 CRLF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44245738/

相关文章:

sql-server - SSIS 接收 Excel 列作为 DT_IMAGE

SQL Server : add foreign key error

php - Laravel Eloquent 区分大小写的关系

java - 使用包含嵌套集合的 pojo 编写 csv 文件

PHP 编码问题读取文件 - ISO-8859-1 和 UTF-8 冲突

sql-server - 如何在 64 位包中执行 32 位 SSIS 包?

sql-server - update-database -force 命令未更新 Entity Framework 代码中的基础 -first

c# - SQL Server 日期时间舍入

MATLAB/Octave - 如何解析包含逗号的数字和字符串的 CSV 文件

SSIS 错误 : [SSIS. 管道] 错误: "component "Excel 源“(14 )"failed validation and returned validation status "VS_NEEDSNEWMETADATA”