What you want to see on DotNetFunda.com ?
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 54060 |  Welcome, Guest!   Register  Login
Home > Articles > Sql Server > Looping through all the files in a folder and loading data to SQL (SSIS)

Looping through all the files in a folder and loading data to SQL (SSIS)

1 vote(s)
Rating: 5 out of 5
Article posted by Madhu.b.rokkam on 2/20/2011 | Views: 7338 | Category: Sql Server | Level: Intermediate | Points: 250 red flag


This article will guide you on how to loop through a folder and process and load all the txt file data into SQL Server using SSIS.

Introduction

If you have gone thru the articles posted by karthik let me guide you further on how to loop thru an input folder and pick files from there and load them into SQL Database.

So Lets start...

Step 1 - Open Visual Studio, Select Integration Services Project template from Create New Project option. You will get Package.dtsx loaded on to the screen.

Step 2 - Drag drop the foreach loop container on to the designer and in it drop one Data Flow Task.


Step 3 -Double click on for each loop container, Select Collection option and configure the values as shown in the figure.

Select Variable Mapping, below the variable option select new variable and name it say CurrentFile to save the filename while looping thru the folder, finally click ok to close the window.

Step 4 -Add two flat file connection manager one for input file and one for error file and one OleDB connection manager to connect to database table.

Step 5 - Configure the inputfile connection manger by specifing the sample file path and then click on columns to preview the data in the file. Then click on advanced tab to set the column datatypes matching the table datatypes then click ok.

Similary set the errorfileconnection manager also.

Then double click on the oledb connection manger point into the table in the database where you want to lad the data.

Step 6 - Double click on the Data Flow Task and Drop one Flat File source, Oledb destination and one file destination.

Double click on each component and point to the respective connection managers.

And in case of OLEDB map the columns as per the columns in the table.



And in the error input tab you need to specify as below



Thats it from the package level.

Lets create a table in the Database

Create table Student(
rollno int primary key,
nm varchar(50),
age int,
sex char(1),
loc varchar(10))

Similarly you can specify some other test files in the input folder.

Then execute the package you will see that the data gets loaded into Table and any error records will be moved to the error file.

This shows that the package got executed successfully.

Conclusion

Hope this article will take you to the next level of SSIS package creations.

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

About Madhu Rokkam

Experience:0 year(s)
Home page:http://www.dotnetfunda.com
Member since:Thursday, January 13, 2011
Level:Bronze
Status: [Member] [MVP]
Biography:
 Responses
Posted by: Tripati_tutu | Posted on: 21 Feb 2011 02:05:39 AM | Points: 25

Better article on SSIS...

Posted by: Madhu.b.rokkam | Posted on: 21 Feb 2011 04:47:52 AM | Points: 25

Will post much much better articles in the comming weeks

>> Write Response - Respond to this post and get points
Related Posts

This article explains how we can generate scripts using SQL Server

This is part 30 of the series of article on SSIS and in this article we are going to see what is Data Flow Transformations in SSIS and the list of controls that are provided in the data flow transformations followed by the series on each of the control and the usage of the controls.

Delete Duplicate rows from the table.

Retrieving Column Names of a table from database using SQL

In this article we will learn Last_Value function of Sql Server 2012 (Denali).

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/23/2013 7:55:09 AM