sql-server - 增加数据流任务中的变量

标签 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 并递增它,然后写入下一条记录。

最佳答案

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

关于sql-server - 增加数据流任务中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58155707/

相关文章:

SQL Server,从不同表的 2 列中选择

vb.net - VB语法和表达错误

mysql - 无法读取 MySql 字符串值

SSIS:固定宽度列无法正确显示

sql-server - SSIS - 如何将保存在不同文件夹中的 csv 文件复制到单个文件夹

SQL Server 触发器将 "false"替换为 null

php - 尽量减少 SQL 语句? - 堆栈 SQL 查询?

sql-server - 将日期转换为参数内的整数

windows - VB.NET - 检查 Windows 许可证状态或正版 Windows

SSIS 服务器维护作业错误