2015-11-03 19 views
2

我有一个包含随机文本行的excel文件。其中一些只包含一个单词,其他的多个而其他仅仅包含NULL。SSIS - 将每个字符串(包括NULL)拆分为新表中的行

现在我试图在SSIS中创建一个数据流,其中我只在一个表中创建一个新的表,并在ID中添加所有单词。

所以:

ID | Text 
1 | food 
2 | *NULL* 
3 | tree car map 
4 | water 

应该改为:

ID | Text 
1 | food 
2 | tree 
3 | car 
4 | map 
5 | water 

我试图用脚本组件(like in this link, what most people suggested on other posts here)做这件事,但没有奏效。 (A pastebin link to my code and my Runtime error here

有什么方法可以解决这个问题?我希望在SSIS中完成100%。

回答

1

问题在于如何在脚本中处理NULL值。 方法Row.Hashtags.ToString().Split(new char[] { ' ' }, StringSplitOptions.None)无法处理NULL值。

要解决此问题,我们可以在使用Split函数之前检查NULL值。与此更换你的代码:

// Method that will execute for each row passing 
public override void Input0_ProcessInputRow(Input0Buffer Row) 
{ 
    //Check if the value is null before string split 
    if (Row.Value_IsNull == true) 
    { 
     Output0Buffer.AddRow(); 
     Output0Buffer.SplitID = Row.ID; 
     Output0Buffer.SplitValue = Row.Value; 
    } 
    else 
    { 
    string[] SplitArr = Row.Value.ToString().Split(new char[] { ' ' }, StringSplitOptions.None); 

     // Counter var used the loop through the string array 
     int i = 0; 

     // Looping through string array with student names 
     while (i < SplitArr.Length) 
     { 
      // Start a new row in the output 
      Output0Buffer.AddRow(); 

      Output0Buffer.SplitID = Row.ID; 

      // This is the splitted column. Take the [n] element from the array 
      // and put it in the new column. 
      Output0Buffer.SplitValue = SplitArr[i]; 

      // Increase counter to go the next value 
      i++; 
     } 
    } 
} 

我使用的投入IDValue,且输出SplitIDSplitValue。将它们重命名为您的选择,但请记住将它们添加到脚本组件中。

0

脚本组件绝对可以工作。但是,您发布的堆栈跟踪中没有错误消息,因此我无法帮助您调试脚本。

然而,我会处理这个问题的方法是将Excel数据“原样”导入到临时表中,然后使用分割函数执行存储过程以将数据传递到最终目标表中。