How to Loop (ForEach) thru "Copy data" and execute Stored Procedure once?

King Java 790 Reputation points
2023-01-24T19:25:16.6433333+00:00

I am using a variable with Array to insert multiple csv files using "ForEach".

Currently, I am testing with 1 and 2 as a Default value.

User's image

I was able to get the "ForEach" working, but now the issue is, because my other activities (CopyData and Stored Procedure) are all inside ForEach, it copies data two times (because I have 1 & 2 as a Default value).

This is current two activities inside "ForEach":

User's image

  1. The "CopyData", basically, brings data from a csv file and inserts data into a Staging table.

It truncates data inside the Staging table first before inserting data from each csv file.

2.The "Stored Procedure" inserts data from the Staging table into Production table.

So the dilemma is that if I locate the Stored Procedure activity outside the ForEach, it only inserts data of one of two csv files.

How do I set up Activities properly so that it would not insert data by the number of files (this time two) and put data into Production table at once/properly (not twice nor only data of one csv file)?

Thanks.

Azure Data Factory
Azure Data Factory
An Azure service for ingesting, preparing, and transforming data at scale.
11,624 questions
0 comments No comments
{count} votes

Accepted answer
  1. Nandan Hegde 36,146 Reputation points MVP Volunteer Moderator
    2023-01-25T03:43:10.2466667+00:00

    Hey,

    1. use a script task outside the for each activity to truncate the staging data
    2. within for each, within copy activity no need to use a pre copy script and let all the files data get appended within the staging data
    3. Use the Sproc activity outside ,post foreach to have a 1 time load from staging to destination table
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.