sql - 可变长度的 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[1];
Row.Age = ColumnValue[2];
但是当我运行 SSIS 包时,我得到一个索引数组错误。我认为这是因为我尝试拆分的字符串并不总是具有 City 和 Age 的值。数据正在从 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];
}
推荐阅读
- python - dict 定义顺序是否保证从左到右?
- javascript - CRUD React App - 无法将数据发布到 SQL 数据库?
- c# - 如何在成功调用 /connect/authorize 时从 IDS4 获取代码?
- r - 离线运行的移动闪亮应用程序,没有连接
- quickbooks - 当我查询类型为“已售商品成本”的帐户时,如何返回 ExpenseAccountRef?
- julia - Julia 没有选择 MySQL 数据库
- python - 复制 3d numpy 数组的最后一列
- google-analytics - GA4 和 GA3 会话计数的差异
- javascript - 在自定义匹配器中测试嵌套对象
- google-cloud-platform - 根据两个条件获取 BigQuery 中每个 user_id 的最新行