3

好的,我对 SSIS 还很陌生,正在尝试将平面文件从一个系统转换为可导入另一个系统的文件。

此文件转换的一部分是使用标题记录。头记录由一些固定组件和一些动态组件组成。动态组件是记录数和支付金额(以下表达式中的“PAYAMT”)。我正在尝试使用标题属性表达式将标题附加到详细记录。

"00 " + REPLICATE("0",6-LEN((DT_STR,6,1252) @[User::RecordCountA1200])) + (DT_STR,6,1252) @[User::RecordCountA1200] + "PAYAMT" + “P1200000000000000000000”

支付金额字段是货币的数据类型。我的第一个想法是使用聚合转换并将其存储在记录集目标中。我的聚合为我提供了正确的总和,但变量只能存储为对象,而不是我最初期望的数字数据类型。我想获取所有记录的 PayAmount 总和,并将其放入名为 SumAmountA1200 的用户定义变量中。

是否可以将聚合转换中的值存储到其他类型的转换中并将其转换为包级变量?我应该以另一种方式去做吗?非常感谢任何反馈

4

1 回答 1

2

第一个选项是使用连接到您的聚合转换的脚本转换。

您只能在数据流的执行前/执行后阶段访问 SSIS 变量。由于此限制,您的脚本将执行Input0_ProcessInputRow事件中的任何逻辑。在您非常具体的情况下,只会发送 1 行,并且您希望将 Row 中的值分配给类范围的成员变量。

在 PostExecute 方法中,您将变量的值分配为成员变量的值。

示例代码

此脚本是充当目标的脚本转换。我已将变量检查为读/写 (User::ExternalVariable),在输入/输出选项卡上,我从聚合 ( Column) 中选择了列。

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;

/// <summary>
/// This is the class to which to add your code.  Do not change the name, attributes, or parent
/// of this class.
/// </summary>
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
    long memberVariable;

    /// <summary>
    /// Can update the package's Variable here
    /// </summary>
    public override void PostExecute()
    {
        base.PostExecute();
        //this.Variables.ExternalVariable = this.memberVariable;
    }

    /// <summary>
    /// Assign a row's value to the class level variable.
    /// Cannot assign to the 
    /// </summary>
    /// <param name="Row">The row that is currently passing through the component</param>
    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {

        this.memberVariable = Row.Column;
        // this results in a runtime error
        // The collection of variables locked for read and write access is not available outside of PostExecute.
        //this.Variables.ExternalVariable = 1111L;
    }

}
于 2013-05-07T03:09:08.837 回答