For Each Loop SSIS. Dependent on SQL Query - sql-server

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

Related

Export a folder of SQL queries into CSV in SSIS

I am a newbie to SSIS and currently struggling with executing SQL task saving the result in a result set and exporting each table to a respective CSV using a data flow task.
There are 15 .sql files stored in a folder which I am creating a variable called FolderPath pointing towards them. Then I create a for each container that reads from a folder and create a variable in the variable mapping which is called SQLfile.
Inside the for-each container I have an execute SQL task which I changed its file connection variable and edited the expression to FolderPath + SQLfile.
Executing this loop works, when the result set value is set to none. Now I am trying to connect the tables created from this loop in a data flow task. I have no idea how to do this but I am guessing it has something to do with the result set. When I change the result set to full result set my loop breaks. I am assuming you cant have a result set inside a loop.
Now I am completely lost as I don't know how to save the result of those 15 tables and how to declare them as source in the data flow task.
Store the query in a variable. Then use the OLE DB Source component in the Data Flow Task and set the 'data access mode' to sql command from variable.

Extract filename and update table in SSIS Foreach Loop Container

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

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.

Why is the value of a variable null inside a foreach loop container

I have a package that has a foreach file enumerator. Now I have a variable called EmailTo which is populated by an execute sql task outside the foreach container placed just before the loop container since i only need to run the execute sql task once. The variable EmailTo is being accessed by an send mail task inside the foreach container. When i inspect the breakpoint. I learned that the value of EmailTo variable is null inside the foreach container. Why is that?
Currently, I moved the execute sql task inside the foreach container to make it work because I have to present it to my boss but there is no point of moving the execute sql task inside the loop since i only have to fetch the email once.
What should I do.
When you store an Execute SQL task Full ResultSet within a variable, it is stored as a Recordset.
A Recordset can be consumed once; if it is used by a Send Mail Task, then you cannot use it again.
You can use a Script Task to copy the Recordset into a DataTable object and store it within an SSIS object variable. Then you can use it multiple times.
Additional Information
SSIS 2012 - Variable empty on second script execution
I have two Foreach Loop,which hava same enumerated values.So how do I deal with it?

Moving files based on a source path found in a table using SSIS

I've chased my tail for a full 12 hours. Haven't found the right solution.
I'm locked into using SSIS. I have a SQL Server table with full paths and filenames already concatenated. Examples:
\\MydevServer1\C$\ABC\App_Data\Sample.pdf
\\MydevServer2\E$\Garth\App_Data\Morefiles.txt
\\MydevServer3\D$\Paths\App_Data\MySS.xlsx
etc.
I need to read each row of the table, get the path and filename and move that file to a new static destination directory.
The rows in the table will remain unchanged. I only use it as a source to locate the file to be moved.
I've tried:
1) Feeding a resultset from an ole db source to a recordset destination then to an Object variable that connects via variable to a foreach loop container holding a files system task. (Very problematic.)
2) Sending the table rows to a .csv file and reading each line of the csv file using a foreach loop container holding a file system task.
3) Reading directly from the table rows using a foreach loop container holding a file system task. (preferred).
and many other scenarios.
I have viewed a hundred examples online, but most of them involve loading a table, or sending results to flat files, or moving files from one folder to another based on extension type, etc. I haven't found anything on configuring a file system task to read a table supplied path and move the file based on the table value as the source.
I'm rambling. :-)
Any insight or help will be appreciated. I'm not new to SSIS, but I sure feel like it right now.
Create two string variables to store source and destination paths
Use an Execute SQL Task to populate a Full Recordset (Variable with Object data type)
Use For Loop container to go through each row of recordset and set those two variables.
Inside For Loop container, use File System Task. You need to specify IsSourcePathVariable = True, IsDestinationPathVariable = True, path variables - DestinationVariable / SourceVariable, and set operation (copy, move, etc.)
It appears I've been tail chasing due to the error, "Source is empty error".
This was caused by a blank first row in my recordset. I was searching for a fix to the Object variable is empty issue, when in reality the issue was that the Object variable couldn't find data right off the bat.
Insert shameful smug here.
Thanks to Anton for the help.

Resources