将subtoal添加为数据流任务SSIS中的新行

时间:2015-05-20 22:26:36

标签: sql-server excel ssis

SSIS数据流任务

Source: Stored Procedure @ SQL Server Dest: Excel

存储过程结果:

User Score Jim 15 Betsy 10

我需要在结果集中添加一个新的,这是我将其转储到excel文件之前的总分数。所以结果应该是这样的:

User Score Jim 15 Betsy 10 Total 25

通过SSIS实现此目的的最佳方法是,有几种方法可以根据现有列计算新列,但我不确定如何添加新行。我们是否有类似于SSRS的聚合函数?我在SSIS中尝试了聚合转换,如下所示:

Score --> Sum User --> Group By

没有给我我想要的东西。想法请。

1 个答案:

答案 0 :(得分:1)

这个解决方案对我来说很好。 通过SSIS脚本任务使用AddRow()方法,这是C#中的3行代码

[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{

    int total = 0;
    public override void PreExecute()
    {
        base.PreExecute();
    }

    public override void PostExecute()
    {
        base.PostExecute();

    }

    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {

        total = total + Row.Score;

        GrandTotalBuffer.AddRow();              // Add a new row
        GrandTotalBuffer.Score = total;         // Assign calculated value of the variable 'total' to the Column 'Score' for the newly added row
        GrandTotalBuffer.User = "Total";        // Assign value 'Total' to the Column 'User' for the newly added row
    }

    public override void CreateNewOutputRows()
    {

    }

}

以下是数据流任务的样子:

enter image description here

包含计算结果Grand Total的{​​{1}}包含来自Output 0的常规行,因此需要使用OLE DB Source