SSIS - 将每个字符串(包括 NULL)拆分为新 table 中的行
SSIS - split each string (incl. NULL) into rows in new table
我有一个 excel 文件,其中包含带有随机文本的行。有些只包含一个词,有些是多个词,有些只是 NULL 。
现在我正尝试在 SSIS 中创建一个数据流,我在 table 中创建了一个新的,只有 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 ), but that didn't work. ( A pastebin link to my code and my Runtime error here)
有什么方法可以解决这个问题?我希望它在 SSIS 中 100% 完成。
脚本组件肯定可以。但是,您发布的堆栈跟踪中没有错误消息,因此我无法帮助您调试脚本。
不过,我的处理方式是将 Excel 数据 "as-is" 导入暂存 table,然后执行带有拆分函数的存储过程以传递数据进入最终目的地 table.
问题在于脚本中如何处理 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++;
}
}
}
我使用了输入 ID
和 Value
,以及输出 SplitID
和 SplitValue
。根据您的选择重命名它们,但请记住将它们添加到脚本组件中。
我有一个 excel 文件,其中包含带有随机文本的行。有些只包含一个词,有些是多个词,有些只是 NULL 。
现在我正尝试在 SSIS 中创建一个数据流,我在 table 中创建了一个新的,只有 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 ), but that didn't work. ( A pastebin link to my code and my Runtime error here)
有什么方法可以解决这个问题?我希望它在 SSIS 中 100% 完成。
脚本组件肯定可以。但是,您发布的堆栈跟踪中没有错误消息,因此我无法帮助您调试脚本。
不过,我的处理方式是将 Excel 数据 "as-is" 导入暂存 table,然后执行带有拆分函数的存储过程以传递数据进入最终目的地 table.
问题在于脚本中如何处理 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++;
}
}
}
我使用了输入 ID
和 Value
,以及输出 SplitID
和 SplitValue
。根据您的选择重命名它们,但请记住将它们添加到脚本组件中。