Azure 数据工厂中的设置变量活动Set Variable Activity in Azure Data Factory

适用于:是 Azure 数据工厂是 Azure Synapse Analytics(预览版)APPLIES TO: yesAzure Data Factory yesAzure Synapse Analytics (Preview)

使用“设置变量”活动可设置数据工厂管道中定义的 String、Bool 或 Array 类型的现有变量的值。Use the Set Variable activity to set the value of an existing variable of type String, Bool, or Array defined in a Data Factory pipeline.

Type 属性Type properties

propertiesProperty 说明Description 必选Required
namename 管道中活动的名称Name of the activity in pipeline yes
descriptiondescription 描述活动用途的文本Text describing what the activity does no
typetype 必须设置为“SetVariable”Must be set to SetVariable yes
value 变量分配到的字符串文本或表达式对象值String literal or expression object value that the variable is assigned to yes
variableNamevariableName 此活动设置的变量的名称Name of the variable that is set by this activity yes

递增变量Incrementing a variable

涉及 Azure 数据工厂中的变量的常见情景是将变量用作 until 或 foreach 活动中的迭代器。A common scenario involving variables in Azure Data Factory is using a variable as an iterator within an until or foreach activity. 在设置变量活动中,无法引用在 value 字段中设置的变量。In a set variable activity you cannot reference the variable being set in the value field. 若要解决此限制,请设置一个临时变量,然后创建第二个设置变量活动。To workaround this limitation, set a temporary variable and then create a second set variable activity. 第二个设置变量活动会将迭代器的值设置为临时变量。The second set variable activity sets the value of the iterator to the temporary variable.

下面是此模式的示例:Below is an example of this pattern:

递增变量Increment variable

{
    "name": "pipeline3",
    "properties": {
        "activities": [
            {
                "name": "Set I",
                "type": "SetVariable",
                "dependsOn": [
                    {
                        "activity": "Increment J",
                        "dependencyConditions": [
                            "Succeeded"
                        ]
                    }
                ],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "i",
                    "value": {
                        "value": "@variables('j')",
                        "type": "Expression"
                    }
                }
            },
            {
                "name": "Increment J",
                "type": "SetVariable",
                "dependsOn": [],
                "userProperties": [],
                "typeProperties": {
                    "variableName": "j",
                    "value": {
                        "value": "@string(add(int(variables('i')), 1))",
                        "type": "Expression"
                    }
                }
            }
        ],
        "variables": {
            "i": {
                "type": "String",
                "defaultValue": "0"
            },
            "j": {
                "type": "String",
                "defaultValue": "0"
            }
        },
        "annotations": []
    }
}

后续步骤Next steps

了解数据工厂支持的相关控制流活动:Learn about a related control flow activity supported by Data Factory: