SSIS - 如何拆分行(而不是列)

时间:2015-08-31 00:45:57

标签: ssis

我有一个包含100行和20列的文件,前两行是文本,另外98行是数字。没有标题 - 我需要将row1和row2组合起来以获得标题。

我想将第1行拆分为一个输出,将第2行拆分为另一个输出,然后将col1组合为row1和row2,将col2组合为row1和row2,将col3组合为row1,将row2组合为所有20列以创建新标题。合并后,我将使用union all写入一个新的输出文件,该文件现在有99行:第1行是带有新标题的文本,其余99是数字。

SSIS允许我这样做吗?我知道它可以对列值进行条件拆分,但有没有办法根据行号或行号拆分值?可以用行条件分割工作吗?

THX

1 个答案:

答案 0 :(得分:0)

我可以想到两个解决方案。

  1. 您可以创建一个“Source”类型的脚本组件来读取文件,这样您就可以在阅读它们时控制行输出。这消除了连接管理器/平面文件组件的使用/好处,因此我不建议将其作为首选。

  2. 您创建了一个类型为Transformation的短脚本组件,只需更改前几行(假设读数是正确的行顺序)。

  3. 对于#2,这是它的外观(你可以过滤掉RowIndex 0): enter image description here 在脚本组件中,确保设置以下内容(创建输出列'RowIndex'并将输入列标记为'ReadWrite'):

    enter image description here enter image description here 在设置之后,脚本只需要是这样的:

    public class ScriptMain : UserComponent
    {
        public static int RowCount = 0;
    
        public override void Input0_ProcessInputRow(Input0Buffer Row)
        {
            if (RowCount == 0)
            {
                // Store your row's information
                string Column0 = Row.Column0;
                string Column1 = Row.Column1;
    
                // Set the index (0), and make it fetch the next row
                Row.RowIndex = RowCount++;
                Row.NextRow();
    
                // With the next row (the second), combine the columns.
                Row.Column0 = Column0 + " " + Row.Column0;
                Row.Column1 = Column1 + " " + Row.Column1;
            }
            // Increment and store for each following row.
            Row.RowIndex = RowCount++;
        }
    }
    

    希望这有帮助。