【问题标题】:Increment Variables in Data Flow task在数据流任务中增加变量
【发布时间】:2019-09-29 13:31:56
【问题描述】:

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

我正在使用条件拆分,它查看行类型(字段 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 并将其递增,然后写入下一条记录。

【问题讨论】:

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


    【解决方案1】:

    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
    

    【讨论】:

    • 谢谢你 - 我可以在脚本之外使用局部变量吗?我需要能够计算出该值并将其作为另一个拆分记录上的外键。
    • @JbP 不,你不能。您必须将此值分配给 PostExecute 方法中的 SSIS 变量。正如我在上面的代码中提到的
    • 是的,对不起,伙计,确实读过,但目前对此有点压力:) 我基本上有一个文件,其中包含许多 AAA 记录,每个记录都应该有自己的 ID,然后在那个下面我有一个 BBB 记录,也需要写入相关的 AAA ID。
    • 我只是不知道该怎么做。
    • @JbP 我认为您应该使用 Lookup TransformationMerge Join 来获取第二个来源的相关 ID。网上有很多例子
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多