Sql 具有可变长度的SSIS脚本拆分字符串

Sql 具有可变长度的SSIS脚本拆分字符串,sql,parsing,split,ssis,Sql,Parsing,Split,Ssis,我试图使用脚本组件拆分SSIS中的分隔字段,并得到一个索引数组错误。如果我的数据集是 Name City Age ----------------------- John Washington 25 Sarah Chicago Mike Mary Philadelphia 34 我正在使用脚本代码 Var ColumnValue = Row.People.Split(' '); Row.Name = ColumnValue[0]; Row.City = ColumnValue

我试图使用脚本组件拆分SSIS中的分隔字段,并得到一个索引数组错误。如果我的数据集是

Name City Age
-----------------------
John Washington 25
Sarah Chicago
Mike
Mary Philadelphia 34
我正在使用脚本代码

Var ColumnValue = Row.People.Split(' ');

    Row.Name = ColumnValue[0];
    Row.City = ColumnValue[1];
    Row.Age = ColumnValue[2];

但是当我运行SSIS包时,我得到一个索引数组错误。我想这是因为我试图拆分的字符串并不总是有城市和年龄的值。数据正在从Excel文件加载到SQL DB,如果缺少这些字段,则字段末尾没有任何空格/分隔符。如何解析此字段?

您盲目地请求一些不存在的内容,但没有警告引擎它可能找不到任何内容

相反,您应该检查数组的结果长度,并按预期填充列

Var ColumnValue = Row.People.Split(' ');
// We assume there's always a first thing
Row.Name = ColumnValue[0];

// Here begins things that might not be there
if (ColumnValue.Length > 1)
{
    Row.City = ColumnValue[1];
}

if (ColumnValue.Length > 2)
{
    Row.Age = ColumnValue[2];
}

谢谢这仍然会给我一个索引数组错误。我认为问题在于该字段以空格分隔,并且该字段已被截断。因此,我使用了“Sarah Chicago”而不是“Sarah Chicago”,因此数组并不总是有三列,因为最后一个分隔符丢失了。@MarkWilson Classic以一个为准-通过小提琴检查它,现在应该可以工作了