2

I have an SSIS package that has this Foreach Loop Container(with File Enumerator) that reads from a folder with multiple CSVs file and then upload the data into a flat table.

This is working fine but my problem is trying to also extract the filenames of the file and then populate the last column in the flat table after inserting a row.

I have also added an execute SQL task after the Data Flow task(within the ForEach Loop Container) hoping that it would execute straight away before the loop goes to the next file, but unfortunately this is not the behavior.

The execute SQL task will only execute after all the data in all the files is read. Is there a way to do this filename update row by row, such as read a row from the CSV file, insert this row into the table, update the row in the filename column in the same table, and then read the next row? Continue this way until the CSV is read completely then move to the next CSV file and do the same.

I have a programming background and slightly feel that nested for loops could be a way but not sure how to achieve this in SSIS. The setup of my ForEach loop container is shown below:

Contro flow

Data flow

Hadi
  • 36,233
  • 13
  • 65
  • 124
eloga
  • 73
  • 7
  • *"The execute Sql task will only execute after all the data in all the files is read"* No it won't, it's *inside* the foreach loop; it'll run after each file is processed. As for inserting the name of the file, add a derived column transformation in your Data Flow, and then add a new column within that, using the variable that contains the file's path. – Thom A Feb 01 '22 at 09:39
  • @Larnu thanks for quick response. Could you explain a bit more on this step. – eloga Feb 01 '22 at 09:52
  • On which step..? – Thom A Feb 01 '22 at 09:55
  • @Larnu I am able to follow with responses below. Thanks a lot for the guide – eloga Feb 02 '22 at 03:50

1 Answers1

1

Why using an Execute SQL Task to add the file name?!

You can simply add the file name into the data pipeline using one of the following methods:

(1) Using the FileNameColumnName property

In the Data Flow Task, you can simply right-click on the Flat File Source, and click on the Show Advanced Editor option.

enter image description here

In the Flat File Source Advanced Editor, there is a property called FileNameColumnName. This property is used to add a column to the flat file source where the File Name is added.

enter image description here

You should only write the value of the column name and it will be added to the flat file source.

enter image description here

(2) Using a Derived Column Transformation

Your issue can be solved by adding a Derived Column Transformation within the Data Flow Task. Then, add a column to the data pipeline using the variable that contains the File Name. (The variable used in the ForEach Loop Container's variables mappings tab)

You can learn more about Derived Column Transformation in the following article:


Similar questions:

Hadi
  • 36,233
  • 13
  • 65
  • 124
  • 1
    I have only ever used option 2. Nice to know new things – KeithL Feb 01 '22 at 17:34
  • @KeithL custom properties are available only in the advanced editor: https://learn.microsoft.com/en-us/sql/integration-services/data-flow/flat-file-custom-properties?view=sql-server-ver15 this is why most developers are not aware of them – Hadi Feb 01 '22 at 17:58