Sql server SSIS-将每个字符串(包括NULL)拆分为新表中的行
我有一个包含随机文本行的excel文件。其中一些只包含一个单词,另一些包含多个单词,而另一些则为空 现在,我正在尝试在SSIS中创建一个数据流,其中我创建了一个新的in表,在一列中只包含和ID以及所有单词 因此: 应成为:Sql server SSIS-将每个字符串(包括NULL)拆分为新表中的行,sql-server,ssis,Sql Server,Ssis,我有一个包含随机文本行的excel文件。其中一些只包含一个单词,另一些包含多个单词,而另一些则为空 现在,我正在尝试在SSIS中创建一个数据流,其中我创建了一个新的in表,在一列中只包含和ID以及所有单词 因此: 应成为: ID | Text 1 | food 2 | tree 3 | car 4 | map 5 | water 我尝试过使用脚本组件()来实现它,但没有成功。() 有办法解决这个问题吗?我希望它是做100%的SSIS 脚本组件肯定可以工作。但是,您发布的堆栈跟踪中不存
ID | Text
1 | food
2 | tree
3 | car
4 | map
5 | water
我尝试过使用脚本组件()来实现它,但没有成功。()
有办法解决这个问题吗?我希望它是做100%的SSIS 脚本组件肯定可以工作。但是,您发布的堆栈跟踪中不存在错误消息,因此我无法帮助您调试脚本
不过,我处理这个问题的方法是将Excel数据“按原样”导入一个暂存表,然后使用拆分函数执行一个存储过程,将数据传递到最终的目标表。问题在于脚本中如何处理
NULL
值。
方法Row.Hashtags.ToString().Split(新字符[]{'},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++;
}
}
}
//将为每一行传递执行的方法
公共覆盖无效Input0\u进程InputRow(Input0Buffer行)
{
//在拆分字符串之前检查该值是否为null
if(Row.Value_IsNull==true)
{
Output0Buffer.AddRow();
Output0Buffer.SplitID=Row.ID;
Output0Buffer.SplitValue=行.Value;
}
其他的
{
string[]SplitArr=Row.Value.ToString().Split(新字符[]{''},StringSplitOptions.None);
//计数器var通过字符串数组使用循环
int i=0;
//使用学生姓名在字符串数组中循环
而(i
我使用了输入ID
和Value
,以及输出SplitID
和SplitValue
。根据您的选择重命名它们,但请记住将它们添加到脚本组件中
// 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++;
}
}
}