SSIS脚本组件合并两行缓冲数据问题求助
解决方案
要实现16行与后续88行的自动合并,需要在脚本组件中缓存前一行的16型数据,并在遇到对应88行时完成合并输出,具体修改步骤如下:
- 在脚本组件的类级别添加缓存变量,用于暂存未完成合并的16行数据:
// 类级别变量,缓存16行数据 private string cached16Row; private bool hasPending16Row = false;
- 修改
ProcessInputRow方法的逻辑,处理行缓存、合并与输出:
public override void ProcessInputRow(Input0Buffer Row) { string recordType = Row.RowData.Substring(0, 2); // 处理16型行:缓存数据,暂不输出 if (recordType == "16") { // 移除末尾的/并清理空格 cached16Row = Row.RowData.TrimEnd('/').Trim(); hasPending16Row = true; return; } // 处理88型行:如果有缓存的16行,执行合并 if (recordType == "88" && hasPending16Row) { // 移除88行开头的"88,",获取后续内容 string commentContent = Row.RowData.Substring(3).Trim(); // 合并16行与88行内容(补充逗号对齐格式) string mergedRow = $"{cached16Row},{commentContent}"; // 输出合并后的行 TransactionDetailOutputBuffer.AddRow(); TransactionDetailOutputBuffer.RowData = mergedRow; // 清空缓存 hasPending16Row = false; cached16Row = null; return; } // 处理其他类型行:先输出缓存的16行(如果有),再输出当前行 if (hasPending16Row) { TransactionDetailOutputBuffer.AddRow(); TransactionDetailOutputBuffer.RowData = cached16Row; hasPending16Row = false; cached16Row = null; } // 输出当前非16/88行 TransactionDetailOutputBuffer.AddRow(); TransactionDetailOutputBuffer.RowData = Row.RowData; }
- 处理文件末尾的残留缓存:添加
FinishOutputs方法,确保最后未匹配到88行的16行也能输出:
public override void FinishOutputs() { // 如果还有未输出的16行,补充输出 if (hasPending16Row) { TransactionDetailOutputBuffer.AddRow(); TransactionDetailOutputBuffer.RowData = cached16Row; } base.FinishOutputs(); }
逻辑说明
- 遇到16行时,先清理末尾的
/并缓存,不直接输出,等待后续是否有88行 - 遇到88行且存在缓存的16行时,移除88行开头的
88,,将剩余内容拼接到16行末尾,输出合并后的完整行 - 遇到其他类型行时,先输出缓存的16行(如果有),再输出当前行
- 处理完所有行后,检查是否有未匹配的16行,确保不会丢失数据
内容的提问来源于stack exchange,提问作者rvphx
相关产品推荐
相关产品推荐

