SSIS将多行合并为单行

时间:2015-07-15 22:28:23

标签: sql-server ssis

我有一个包含6列的平面文件:NoteID,Sequence,FileNumber,EntryDte,NoteType和NoteText。 NoteText列有200个字符,如果注释超过200个字符,则文件中的第二行包含注释的延续。它看起来像这样:

|NoteID |  Sequence |   NoteText              |
 ---------------------------------------------
|1234   |    1      |   start of note text... |

|1234   |    2      |   continue of note....  |

|1234   |    3      |   more continuation of first note... |

|1235   |    1      |   start of new note.... |

我如何在SSIS中将多行NoteText组合成一行,这样行就是这样的:

| NoteID      |  Sequence |   NoteText              |
 ---------------------------------------------------
|1234         |    1      |   start of note text... continue of note... more continuation of first note... |

|1235         |    1      |   start of new note.... |

非常感谢任何帮助?

更新:将SynchronousInputID更改为None暴露了Output0Buffer,我可以使用它。以下就是我现在所拥有的。

Dim NoteID As String = "-1"
Dim NoteString As String = ""
Dim IsFirstRow As Boolean = True
Dim NoteBlob As Byte()
Dim enc As New System.Text.ASCIIEncoding()
Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
    If Row.NoteID.ToString() = NoteID Then
        NoteString += Row.NoteHTML
        IsFirstRow = True
    Else
        If IsFirstRow Then
            Output0Buffer.AddRow()
            IsFirstRow = False
        End If
        NoteID = Row.NoteID.ToString()
        NoteString = Row.NoteHTML.ToString()
    End If
    NoteBlob = enc.GetBytes(NoteString)
    Output0Buffer.SingleNoteHTML.AddBlobData(NoteBlob)
    Output0Buffer.ClaimID = Row.ClaimID
    Output0Buffer.UserID = Row.UserID
    Output0Buffer.NoteTypeLookupID = Row.NoteTypeLookupID
    Output0Buffer.DateCreatedUTC = Row.DateCreated
    Output0Buffer.ActivityDateUTC = Row.ActivityDate
    Output0Buffer.IsPublic = Row.IsPublic
End Sub

我现在的问题是我必须将输出列从Wstr(4000)转换为NText,因为有些音符太长了。当它导入我的SQL表时,它只是乱码而不是实际的音符。

1 个答案:

答案 0 :(得分:1)

在SQL Server Management Studio中(使用SQL),您可以使用stuff函数轻松地将NoteText字段与XML Path结合使用,将行值合并到单个列中,如下所示:

select distinct 
        noteid,
        min(sequence) over (partition by n.noteid order by n.sequence) as sequence,
        stuff((select ' ' + NoteText
                      from notes n1
                      where n.noteid = n1.noteid
                      for xml path ('')
                      ),1,1,'') as NoteText
from notes n;

您可能希望在SSIS中查找类似的内容。查看此链接,了解如何在SSIS中创建脚本组件以执行类似的操作:SSIS Script Component - concat rows

SQL Fiddle Demo