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 |
[!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.
To use a Set Variable activity in a pipeline, complete the following steps:
-
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.":::
-
Search for Set Variable in the pipeline Activities pane, and drag a Set Variable activity to the pipeline canvas.
-
Select the Set Variable activity on the canvas if it is not already selected, and its Variables tab, to edit its details.
-
Select the variable for the Name property.
-
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.":::
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 |
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.
Learn about another related control flow activity: