Extract filename and update table in SSIS Foreach Loop Container - sql-server

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:

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.
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.
You should only write the value of the column name and it will be added to the flat file source.
Flat File Custom Properties
Extract the File Name in SSIS Data Flows using the FileNameColumnName Property
(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:
SSIS Derived Columns with Multiple Expressions vs Multiple Transformations
Similar questions:
How to find which flat file contains data errors while loading multiple flat files using Foreach File enumerator in SSIS

Related

How to find which flat file contains data errors while loading multiple flat files using Foreach File enumerator in SSIS

I have a question in SSIS.
For an instance, I have 100 flat files of the same metadata columns to be loaded using an incremental load, but my question is how can we find which flat contains error data while loading using for each loop container. Any solution can be appreciated
Simplest solution: Since you are using a Foreach Loop container, then the file path is mapped to a variable. You can simply add a Derived Column Transformation and use this variable within the expression as following (assuming the variable name is FilePath):
#[User::FilePath]
Then insert it with the erroneous rows.

For Each Loop SSIS. Dependent on SQL Query

I have an SSIS package which checks for the unprocessed file present in a tracking table and then processed it. Till date only one file would come in and we would process it and as such the process was designed accordingly.
However now multiple files can come in one go and we store those multiple files in the tracking table and we have a column which keeps a track of the unprocessed file.
I am trying to use the For Each loop to process all the unprocessed file. So I get the count of the unprocessed files and would like to simply tun the Point 1 by passing a parameter to the step 1 but I have not been successful in doing it using Foreach From Variable Enumerator. Am I missing something ?
You can do this using the following steps:
Add an Execute SQL Task to get unprocessed files and store the resultset inside a variable of type System.Object
Add a Foreach loop container, change the type to ADO enumerator and select the variable as source
In the variable mapping tab map the result (each file path) to a variable of type string
Inside the foreach loop container add a dataflow task that contains the Flat File source and implement the processing logic you need
Add a flat file connection manager define the columns
Click on the flat file connection manager, press F4 to show the property tab, go to expression.
Select the connectionstring property and use the variable that holds the filepath as expression
Detailed articles
Implementing Foreach Looping Logic in SSIS
Looping Through a Result Set with the ForEach Loop
Using SSIS to Loop Over Result Set and Dynamically Generate Output Files
How to loop through full result set using foreach container in SSIS

SSIS ForEach File loop - insert file name to table

I'm building a SSIS package (using VS 2017) to load a bunch of CSV files from a specific folder. It works great using the ForEach File loop. Data Flow task has a Flat File Source and OLE DB Destination. I want to be able to save the filename in the same table, along the data from the CSV file. How can I do this??
thanks a lot for your time and help
One other method if you want to load the entire file path of the flat files is
Right click on Flat File Source in Data Flow tab
Click "Show Advanced Editor"
Click "Component Properties"
Under Custom Properties you will find "FileNameColumnName".
If you give a name (e.g.: FlatFileName) then that will appear as one of the source output columns in mapping that contains the file name value(full path with file name). It can be mapped to any available column in destination or modified using Derived column task to get only file name.
In my development, I have stored mostly the entire path which helped me in better tracking.
One other method for anyone who comes across this question, instead of using ForEach loop you can also use a simpler method
Right click in Connection Manager
Click on "New Connection"
Select "MULTIFLATFILE" connection type and click Add.
In Connection manager editor, insert location and use wildcard * (E.g.: \\ABC\XYZ\file_*.txt) to pick all the flat files in that folder.
This automatically loops through all the flat files in that folder.
Hope this helps!
The ForEach File Enumerator can capture the file name and assign it to a variable, which can then be routed to the connection string variable of a connection manager for dynamic loading. This same methodology can be applied to write the file name to a database table.
In the data flow, add a Derived Column transformation and add a new column called 'FileName' (or whatever) and then set it's value to the variable value that the ForEach File Enumerator is setting for the file name.

How to add column names to the flat files without column headers using SSIS.

I have multiple flat files containing 126 columns , but each of them is without column names. How should i add column names to these files using SSIS. These files are needed to be imported using SSIS so that i can perform transformation on these files.
Do you want to create a new file which has column names or just assign field names to the columns for use in the rest of the package?
Whichever way, if the input file does not contain column names then set them up as follows…
Create a dataflow task and in the dataflow task create a flat file source.
Configure the flat file source and create a new Flat File Connection Manager
Browse to the input file you want and un-tick the Column Names In First Row
Select Advanced and change all of the default names (Coulmn 0, Coulmn 1 etc) into the field names (and types) you want.
Click OK
If you need to create a new file that has the column names in it, just create a flat file destination and this time have the Column Names In First Row turned on, wire it up to the input you created and save it to a new file
One way to do this (maybe not the quickest way) is by using the Advanced Editor.
Right click on the Excel Source component and select Show advanced editor.
In the new window, you need to go to the Input and Output Properties. You should have this by now:
Click on a column under Output Columns (F1,F2,...)
In Common properties, edit the Name to what you want.
I added a derived column component as my next step and this is what I see under the aviable columns:
As you can see, F1 (which I edited in step 2) has a new column name now.
Edit: I somehow assumed you needed this for Excel. Anyways, I hope it helps.

SSIS Dynamic Mapping column

I'm little new to SSIS and I have a need to import some flat files into SQL tables in the same structure.
(Assume the table is already exist in the same structure and table name and flat file name is same)
I thought to create a generic package (sql 2014) to import all those file by looping through a folder.
I try to create a data flow task in a foreach loop container in the data flow task I dropped a flat file source and ADO.Net destination .
I have set the file source to a variable so that every time it loops through it get the new file. similarly for the ADO.net table name I set it to a the variable so that each time it select a different table according to the file name.
since both source column names and destination column names are same I assume it will map the columns automatically.
but with a simple map it didn't let me to run the package so added a column on the source and selected a table and mapped it.
when I run the package I assumed it will automatically re map everything.
but for the first file it ran but second file it failed complaining with map issues.
can some one let me know whether this is achievable by doing some dynamic mapping?? or using any other way.
any help would be much appreciated.
thanks
Ned

Resources