You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在ForEach循环中将多次Lookup输出合并到单个变量

解决ADF中ForEach循环合并Lookup输出到单个变量的方案

核心操作步骤

  1. 创建数组类型变量
    在管道的Variables中新增一个数组变量(比如命名为combinedResults),初始值设为[]。

  2. 设置ForEach为顺序执行
    务必将ForEach活动的Sequential属性设为true,避免并发更新变量导致的结果覆盖问题。

  3. 循环内实现分批查询与结果追加
    在ForEach循环内部依次添加两个活动:

    • Lookup活动:编写分页SQL查询实现分批读取,示例SQL:
      SELECT * FROM YourTargetTable 
      OFFSET @{variables('currentOffset')} ROWS 
      FETCH NEXT 5000 ROWS ONLY
      
      注意关闭Lookup的「First row only」选项,确保返回完整的批次结果数组。
    • Set Variable活动:更新combinedResults变量,将当前Lookup的结果追加到已有数组中,表达式为:
      @concat(variables('combinedResults'), activity('YourLookupActivityName').output.value)
      
  4. 维护分页偏移量(可选)
    新增整数变量currentOffset,初始值设为0。在每次追加结果后,再添加一个Set Variable活动更新偏移量:

    @variables('currentOffset') + 5000
    

管道JSON关键修改点

  • 在variables节点添加数组变量:
    "variables": {
        "combinedResults": {
            "type": "Array",
            "defaultValue": []
        },
        "currentOffset": {
            "type": "Int",
            "defaultValue": 0
        }
    }
    
  • ForEach活动设置sequential: true:
    "activities": [
        {
            "name": "ForEach_Batch",
            "type": "ForEach",
            "typeProperties": {
                "isSequential": true,
                ...
            }
        }
    ]
    
  • 循环内的Set Variable活动配置:
    {
        "name": "Set_Variable_CombineResults",
        "type": "SetVariable",
        "typeProperties": {
            "variableName": "combinedResults",
            "value": "@concat(variables('combinedResults'), activity('Lookup_BatchData').output.value)"
        }
    }
    

循环执行完成后,combinedResults变量就会包含所有批次查询到的记录集合。

内容的提问来源于stack exchange,提问作者user25299890

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.23 09:43:17