SSIS派生列 - 是否有一种修剪400多列的好方法?

时间:2015-10-30 20:56:28

标签: ssis

我从固定宽度的平面文件导入,它有486列。每个人都需要在进入数据库之前进行修剪。我知道派生列允许我一次做一个,但有没有办法做一个质量rtrim(ltrim(在列上?

1 个答案:

答案 0 :(得分:0)

Two ways you could do it (both BIML ways assumed you've gathered some metadata about column names):

1 - By Script

Two ways here also: With the InputBuffer, or with BIML by row. The function for the inputbuffer is provided and I'm sure you can create your own for the row.

public class ScriptMain : UserComponent
{
  private PipelineBuffer inputBuffer;
  public override void PreExecute()
  {
    base.PreExecute();
  }
  public override void PostExecute()
  {
    base.PostExecute();
  }

  public override void ProcessInput(int InputID, Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer Buffer)
  {
    // We need access to the PipelineBuffer which isn't exposed in ProcessInputRow
    inputBuffer = Buffer;
    base.ProcessInput(InputID, Buffer);
  }

  public override void Input0_ProcessInputRow(Input0Buffer Row)
  {
    StripLeadingTrailingWhitespaceByInputBuffer(inputBuffer);

    // Now for the BIML
    <#
      foreach (var column in table.Columns)
      {
        var colName = column.Name.ToString().Replace("_","");
    #>
    Row.<#= colName #> = StripLeadingTrailingWhitespaceByString(Row.<#= colName #>);
    <#
        }
      }
    #>
  }

  public void StripLeadingTrailingWhitespaceByInputBuffer(Microsoft.SqlServer.Dts.Pipeline.PipelineBuffer inputBuffer)
  {
      for (int i = 0; i < inputBuffer.ColumnCount; ++i)
      {
          if (inputBuffer[i] != null)
              inputBuffer[i] = inputBuffer[i].ToString().Trim();
      }
  }
}

2 - Derived Column BIML

foreach (var column in table.Columns)
{
    var colName = column.Name;
    #>                              
    <DerivedColumns Name="(DCO) StripWhitespace">
        <Columns>
          <Column Name="derive_<#= colName #>" DataType="AnsiString" Length="255">StripWhitespace(<#= colName #>)</Column>
        </Columns>
    </DerivedColumns>
    <#
  }
#>