数据流任务中的增量变量

时间:2019-09-29 13:31:56

标签: sql-server vb.net ssis etl script-component

我有一个很大的文本文件,其中有多个不同的行。

我正在使用条件拆分,它查看行类型(字段1),然后执行一个操作,主要是尝试增加变量,将单行拆分为多列(派生),然后将结果写入表中

但是,当尝试增加变量时,我收到“在PostExecute之外不可用的用于读写访问的变量集合。”

正在使用脚本组件更新该变量。

我曾尝试将代码移至PostExecute,但是到那时,它似乎从未增加。


Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

    Dim MaximumKey As Int32 = Me.Variables.SPIParentID ' Grab value of MaxKey which was passed in

    ' NextKey will always be zero when we start the package.
    ' This will set up the counter accordingly
    If (NextKey = 0) Then
        ' Use MaximumKey +1 here because we already have data
        ' and we need to start with the next available key
        NextKey = MaximumKey + 1
    Else
        ' Use NextKey +1 here because we are now relying on
        ' our counter within this script task.
        NextKey = NextKey + 1
    End If

    'Row.pkAAAParentID = NextKey ' Assign NextKey to our ClientKey field on our data row
    Me.Variables.SPIParentID = NextKey
End Sub

我希望能够使用已有的条件拆分在文件中循环,然后当它达到某个记录类型时,它将采用当前的RecordTypeID并对其进行递增,然后写出到下一条记录。 / p>

1 个答案:

答案 0 :(得分:2)

SSIS变量值不能在数据流任务内更改,该值在执行整个数据流任务后更改。每当您尝试从此变量读取值时,执行数据流任务时它将返回其值。您可以在脚本中更改使用局部变量来实现相同的逻辑:

Dim MaximumKey As Int32 = 0 

Public Overrides Sub PreExecute()

    MyBase.PreExecute()

    MaximumKey = Me.Variables.SPIParentID ' Read the initial value of the variable

End Sub

Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)

    ' NextKey will always be zero when we start the package.
    ' This will set up the counter accordingly
    If (NextKey = 0) Then
        ' Use MaximumKey +1 here because we already have data
        ' and we need to start with the next available key
        NextKey = MaximumKey + 1
    Else
        ' Use NextKey +1 here because we are now relying on
        ' our counter within this script task.
        NextKey = NextKey + 1
    End If

    'Row.pkAAAParentID = NextKey ' Assign NextKey to our ClientKey field on our data row
    Me.Variables.SPIParentID = NextKey
End Sub

Public Overrides Sub PostExecute()

    MyBase.PostExecute()

    Me.Variables.SPIParentID = MaximumKey ' Save the latest value into the variable

End Sub