2017-08-18 7 views
0

SSISを使用して、大部分の場合はまっすぐな.txtファイルをインポートしています。SSISマージ可変列

インポートされるファイルには、最大1ポイントまでの列が設定されていますが、フリーテキスト/コメントフィールドがあります。これは、未知の長さに繰り返すことができます。私は理想的(SSIS内)やりたい何

"000001","J Smith","Red","Free text here" 
    "000002","A Ball","Blue","Free text here","but can","continue" 
    "000003","W White","Green","Free text here","but can","continue","indefinitely" 
    "000004","J Roley","Red","Free text here" 

は単数列として最初の3つの列を維持することが、単一の列に任意のフリーテキストのものをマージすることです。つまり、 'color'列の後に表示されるものをマージ/連結します。

私はSSMSテーブルにこれをロードするときに、次のように表示されます:私はどんな簡単な解決策が表示されない

000001 | J Smith | Red | Free text here          | 
000002 | A Ball | Blue | Free text here but can continue     | 
000003 | W White | Green | Free text here but can continue indefinitely  | 
000004 | J Roley | Red | Free text here          | 

答えて

0

。あなたは、以下のような何かを試すことができます(任意の区切り文字なし)一時テーブルへ

1.ロード完全な生データを:

ステップ:

  1. SQL実行中に一時テーブルを作成します。タスク
  2. フラットファイルソース(Ragged Right形式)と
  3. OLEDBの宛先(以前のusint #tempテーブルの作成)でデータフロータスクを作成する接続マネージャ

ためのタスク)

  • 接続マネージャのためのdelayValidation=True設定し、DFT
  • 設定retainSameConnection=Trueは、一時テーブルとそれを使用して作成するthisを参照してください。

    2. 3列(下記のようなもの)

    with col1 as (
        Select 
        [Val], 
        substring([Val], 1 ,charindex(',', [Val]) - 1) col1, 
        len(substring([Val], 1 ,charindex(',', [Val]))) + 1 col1Len 
        from #temp 
    ), col2 as (
        select 
        [Val], 
        col1, 
        substring([Val], col1Len, charindex(',', [Val], col1Len) - col1Len) as col2, 
        charindex(',', [Val], col1Len) + 1 col2Len 
        from col1 
    ) select col1, col2, substring([Val], col2Len, 200) as col3 
    from col2 
    

    T-SQLの出力分離するために、T-SQLを作成します。

    col1 col2 col3 
    "000001" "J Smith" "Red","Free text here" 
    "000002" "A Ball" "Blue","Free text here","but can","continue" 
    "000003" "W White" "Green","Free text here","but can","continue","indefinitely" 
    

    3。異なるデータフロータスクのOLEDBソースで上記クエリを使用する

    要件に応じて二重引用符( ")を置き換えます。

  • 0

    これは楽しい演習だった:

    は、スクリプトコンポーネントを追加し、データフロー

    を追加します(ソースを選択)

    すべての種類の文字列を出力ID、名前色、FreeTextのに4列を追加します。

    編集スクリプト:

    がトップ、以下の名前空間を貼り付けます。

    using System.Text.RegularExpressions; 
    using System.Linq; 
    

    はCreateNewOutputRowsに次のコードを貼り付け:

    string strPath = @"a:\test.txt"; \\put your file path in here 
        var lines = System.IO.File.ReadAllLines(strPath); 
    
        foreach (string line in lines) 
        { 
         //Code I stole to read CSV 
         string delimeter = ","; 
    
         Regex rgx = new Regex(String.Format("(\"[^\"]*\"|[^{0}])+", delimeter)); 
         var cols = rgx.Matches(line) 
             .Cast<Match>() 
             .Select(m => m.Value.Trim().Trim('"')) 
             .Where(v => !string.IsNullOrWhiteSpace(v)); 
         //create a column counter 
         int ctr = 0; 
    
         Output0Buffer.AddRow(); 
    
         //Preset FreeText to empty string 
         string FreeTextBuilder = String.Empty; 
    
         foreach(string col in cols) 
         { 
    
          switch (ctr) 
          { 
           case 0: 
            Output0Buffer.ID = col; 
            break; 
           case 1: 
            Output0Buffer.Name = col; 
            break; 
           case 2: 
            Output0Buffer.Color = col; 
            break; 
           default: 
            FreeTextBuilder += col + " "; 
            break; 
          } 
          ctr++; 
         } 
    
         Output0Buffer.FreeText = FreeTextBuilder.Trim(); 
    
        }