Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SSIS - How to loop through files in folder and get path+file names and finally execute stored Procedure with parameter as Path + Filename

Tags:

ssis

ssis-2012

Any help is much appreciated. I am trying to create an SSIS package to loop through files in the folder and get the Path + filename and finally execute the stored proc with parameter as path+filename. I am not sure how to get the path+filename and insert the into the Stored proc as parameter. I have attached the screenshot for your reference:

like image 992
Sai Avatar asked Jul 01 '16 17:07

Sai


People also ask

How to loop through the files in SSIs?

Below are the steps we need to perform to loop through the files. Create an SSIS Project and drag the Foreach Loop Container to the control flow. Here we are using 3 variables, one for the full path to the file, the other for extension and the third one to specify the folder.

How do I process a file in a specific folder in SSIs?

Processing files in a specific folder in DTS takes a good amount of work. This same process in SSIS is much easier. You can use a Foreach Loop container in SSIS to define a control flow task to loop through different types of enumerators, such as files, in a specified folder.

What are the properties under retrieve file names in SSIs foreach loop?

List of properties under Retrieve File Names option in SSIS ForEach Loop File Enumerator are: Name and Extension: If you select this option, the Foreach loop will store File name and extension in the variable. For example, Employee1.txt Name Only: If you choose this option, the Foreach loop will store Filename in the variable.

What is SSIs foreach loop enumerator?

The SSIS ForEach Loop container will repeat the control flow task for N number of times, which is similar to Foreach loop in any programming language. The SQL Server Integration Services provides 8 types of enumerators and In this article we will show you the steps involved in configuring the SSIS ForEach Loop File Enumerator.


2 Answers

Looks like you have the right idea in general and the link @Speedbirt186 provided has some good details but it sounds like there are a couple of nuances that I thought I might point out in regards to flow and variables.

The foreach loop can assign the entire path or the file name or file name & extension to a variable. The latter will be the most help in your case if you don't want to add a script task to split the Filename from the path. If you start by adding 5 variables to your project it will make it a little easier. 1 will be the Source Directory Path, another the Destination (Archive) Directory Path, and then 1 to hold the File Name and Extension assigned by the for each loop. Then 2 additional dynamic variables that simply combine the source directory and file name to get the source full path and the destination with file name to get the destination full path.

enter image description here

Next make sure you set up your database and Excel file connections. In your Excel file connection after setting it up go to Expressions in the properties window and set the "Connection String" property to SourceFullPath. This will tell the connection to change the file path at every iteration of your loop.

enter image description here

Now you just need to setup your loop etc. Add the fore each loop container setting a directory, filter, and choose File Name and Extension.

enter image description here

Now in the expression box on the collection page set the directory property to be that of your Source Directory variable.

enter image description here

The last part of the Fore each loop is to set your variable mappings to store the file name in your variable. so go to that tab choose your file name variable and set index to 0.

enter image description here

At this point you can add your data flow and setup your import just like you would with a normal file (note your default value for your file name parameter should be to an actual file with the structure you will want to import).

After your data flow drop in your Execute SQL task and set it up how you need. here is an example of direct input and you can see an easy way to reference a parameter is simply a question mark (?).

enter image description here

Next in your sql task setup your parameter mapping by adding in the details you need such as:

enter image description here

Now you are on to your file task. Drop your file task and setup as you desire, but choose your destination and source full path variables to tell the task which file to move.

enter image description here

that's it your are done. there is 1 more thing to note though. The way you have your precedence set in the image you posted you show going from your data flow to your sql and to your file task simultaneously. If your stored procedure relies on your file you may want to put it after your sql task. You can always change the constraint options to "completion" if you want to move the file even if your stored proc fails.

enter image description here enter image description here

like image 83
Matt Avatar answered Sep 19 '22 23:09

Matt


What you want to do is to create a variable in your package, call it something like Filename. In the Edit window of the Foreach you can configure that variable to be set (on the Variable Mappings page- set index to 0).

To create a variable, you will need to have the Variables window showing. Use the View menu to show it if it's not currently open.

Then when calling your stored procedure you can pass the then current value of the variable as a parameter.

This link might help: https://www.simple-talk.com/sql/ssis/ssis-basics-introducing-the-foreach-loop-container/

like image 39
SvenAelterman Avatar answered Sep 18 '22 23:09

SvenAelterman