top of page

How to avoid variable mix-ups in Data Factory foreach loop?

The foreach activity in Azure data factory is one of the most useful activities. It gives ADF the power to run generic actions against multiple objects. for example, to copy multiple tables from source to target, based on a table list.

But if you want to use variables to store the values changed between iterations be careful. You might not get what you expect.


To demonstrate this, I'll create a simple pipeline that copies rows from one SQL table to another.

My rows are just some letters:

ree












And my pipeline is built like this:

ree
  • a lookup activity to retrieve my source table

  • a foreach activity to run on each row from the lookup (5 iterations)

  • inside the foreach activity - set the letter to a variable

  • inside the foreach activity - write the letter to the destination table


My destination table should look just like the source table, right?

Running the pipeline successfully, and these are the results:

ree










What just happened?


Unlike traditional programming languages, where loop iterations run in serial (one after the other), the foreach activity in data factory runs in parallel. This is done to boost performance so that activities will run at the same time and the pipeline will finish faster.

But that might cause an issue if you assign values and then use these values in variables. There is only one instance of the variable and each loop iteration will change it at the same time, which will cause unexpected values. In the example above, each iteration changed the value of the variable to the respective letter, until the last one changed it to "E", and on the next activity "E" was written to the destination table on all iterations.


How can we work around this behavior?


Option 1- use serial run

You can force the foreach activity to run in serial instead of parallel and then each variable change will occur before the table write, with no mix-up.

This pipeline will, of course, run slower.

To do that, stand on the foreach activity and click settings, then check the "sequential" box.

ree

Option 2 - Don't use variables

You can use the value return from the iteration directly, without going through a variable.

To do that, where you want to use the variable, simply use item().field (field being the name of the column).

for example, on the write to SQL activity:

ree

Instead of using:

@concat('insert into dbo.destination_letters(letter) values(',
    '''',
    variables('v_letter'),
    ''')')

Use:

@concat('insert into dbo.destination_letters(letter) values(',
    '''',
    item().letter,
    ''')')

So, variables are very useful, and foreach is very useful, just make sure they play nicely with one another.

4 Comments


shella
Aug 19

When the beat drops in Sprunki Incredibox, it's like the world stops for a second. Pure musical joy!

Like

jonsen
Aug 18

Hi there, I had this plan to get myself a new watch, but every time I played online it ended the same way — losing small amounts without progress. Out of curiosity I opened https://plinko.co.za, gave it another try and after pushing a bit harder I landed a win big enough to cover my earlier losses. The excitement was real, and now my savings look much better. Honestly, I’d recommend giving it a chance.

Like

feritto
Jun 21

After upgrading my phone, iCloud wouldn’t sync my old notes, and I couldn’t figure out if it was an account glitch or just a setting I missed. I wasted hours toggling things on and off, restarting devices, you name it. Eventually, I started digging for support options and came upon a bunch of feedback while browsing icloud phone number details that gave me a better idea of what to expect from customer service. Turns out, others had similar syncing issues after recent iOS updates. Knowing that helped me push for a proper fix instead of just resetting everything blindly.

Like

Guest
Feb 04

The competitive nature of basketball stars creates an exhilarating experience in every match, as players are required to think quickly and respond to their opponent’s moves in real-time.

Like

STAY IN TOUCH

Get New posts delivered straight to your inbox

Thank you for subscribing!

bottom of page