问题:循环脚本组件中的SSIS包数据转换

时间:2013-08-15 08:04:30

标签: sql sql-server-2008-r2 ssis dts

我在SQL Server 2008 R2中开发了一个包。我想使用脚本块转换数据。但我得到的错误是第一个和最后一个记录未插入,它试图插入违反完整性约束的null。下面是我在VB脚本中的代码。

    <microsoft.sqlserver.dts.pipeline.ssisscriptcomponententrypointattribute()> _
    <clscompliant(false)> _

    Public Class ScriptMain
    Inherits UserComponent
    Dim i As Integer = 0

    Public Overrides Sub PreExecute()
        MyBase.PreExecute()        
    End Sub

    Public Overrides Sub PostExecute()
        MyBase.PostExecute()        
    End Sub
    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

            i = i + 1
            With Output0Buffer
                .OrderHeaderID = i
                .CustomerNumber = Row.CUSTNO
                .OrderNumber = Row.ORDER
                .TermsCode = Row.TRMSCD
                .TermsDescription = Row.TRMDSC
                .TotalLines = Row.TOTLNS
                .TotalDollars = Row.TOTDOL
                .ComCls = Row.COMCLS
                .DropShip = Row.DRPSHP
                .DropshipPurchaseOrder = Row.DSPONO
                .InvoiceNumber = Row.INVNO
                .LstSeq = Row.LSTSEQ
                .OrderType = Row.ORDTYP
                .PONumber = Row.PONUMB
                .ProgramCode = Row.PROGCD
                .ProgramDeal = Row.PROGDL
                .ProgramName = Row.PRGNAM
                .PRONumber = Row.PRONUM
                .ReceivedVia = Row.RCVVIA
                .StatusMessage = Row.STAMSG
                .TermsDescription = Row.TRMDES
                .TermsDiscount = Row.TRMDSC
                .TruckName = Row.TRKNAM

                If IsDate(Row.SHPDAT) Then
                    .ShipDate = Row.SHPDAT
                End If

                If IsDate(Row.ORDDAT) Then
                    .OrderDate = Row.ORDDAT
                End If

                If IsDate(Row.INVDAT) Then
                    .InvoiceDate = Row.INVDAT
                End If

                If IsDate(Row.DATING) Then
                    .Dating = Row.DATING
                End If

                If Not Row.INVDAT_IsNull Then
                    .AddRow()
                Else
                    Exit Sub
                End If
            End With

    End Sub

    Public Overrides Sub CreateNewOutputRows()

        Output0Buffer.AddRow()

    End Sub
End Class

目前我正在尝试插入10行,但它会出错。请指导我。

1 个答案:

答案 0 :(得分:1)

初始化变量i = 0,然后在处理该行之前递增它。这可能会导致输入中的第一行被跳过。