Skip to content

Files

Latest commit

251a97f · Dec 6, 2021

History

History
112 lines (94 loc) · 4.77 KB

control-flow-set-variable-activity.md

File metadata and controls

112 lines (94 loc) · 4.77 KB
title titleSuffix description ms.service ms.subservice ms.custom ms.topic ms.date author ms.author ms.reviewer
Set Variable Activity
Azure Data Factory & Azure Synapse
Learn how to use the Set Variable activity to set the value of an existing variable defined in an Azure Data Factory or Azure Synapse Analytics pipeline.
data-factory
orchestration
synapse
conceptual
09/09/2021
chez-charlie
chez
jburchel

Set Variable Activity in Azure Data Factory and Azure Synapse Analytics

[!INCLUDEappliesto-adf-asa-md]

Use the Set Variable activity to set the value of an existing variable of type String, Bool, or Array defined in a Data Factory or Synapse pipeline.

Create an Append Variable activity with UI

To use a Set Variable activity in a pipeline, complete the following steps:

  1. Select the background of the pipeline canvas and use the Variables tab to add a variable:

    :::image type="content" source="media/control-flow-activities-common/add-pipeline-array-variable.png" alt-text="Shows an empty pipeline canvas with the Variables tab selected having an array type variable named TestVariable.":::

  2. Search for Set Variable in the pipeline Activities pane, and drag a Set Variable activity to the pipeline canvas.

  3. Select the Set Variable activity on the canvas if it is not already selected, and its Variables tab, to edit its details.

  4. Select the variable for the Name property.

  5. Enter an expression to set the value. This can be a literal string expression, or any combination of dynamic expressions, functions, system variables, or outputs from other activities.

    :::image type="content" source="media/control-flow-set-variable-activity/set-variable-activity.png" alt-text="Shows the UI for a Set Variable activity.":::

Type properties

Property Description Required
name Name of the activity in pipeline yes
description Text describing what the activity does no
type Must be set to SetVariable yes
value String literal or expression object value that the variable is assigned to yes
variableName Name of the variable that is set by this activity yes

Incrementing a variable

A common scenario involving variables is using a variable as an iterator within an until or foreach activity. 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:

:::image type="content" source="media/control-flow-set-variable-activity/increment-variable.png" alt-text="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": []
    }
}

Variables are currently scoped at the pipeline level. This means that they are not thread safe and can cause unexpected and undesired behavior if they are accessed from within a parallel iteration activity such as a foreach loop, especially when the value is also being modified within that foreach activity.

Next steps

Learn about another related control flow activity: