Import multiple flat files to multiple SQL Tables

5.3k views Asked by At

Here's my folder setup.

Folder Structure

Here's the File setup

File Structure

The idea is to traverse through the folders & put FileA contents to Table FileA.dbo on the database ( also FileB,FileC etc). The FileName structure is same throughout all the folders.

I have this ssis package where I parse through the folders with a foreachloop-> dataflow.

ssis1 ssis2

I have checked that the algorithm I've formulated to get the filename is working

REVERSE(Substring(Reverse( @[User::FileName] ),5,LEN( @[User::FileName] ))) == "FileA"

It parses out the .txt extension. Below is the setup of the foreach loop I have. Foreach

To start this whole package I've .txt files inside the folder & I make the foreach container loop subfolders.

startingpoint

Since, I'm not really a SSIS package developer, this is the best I could do with some research. The problem I'm running into is, it seems to work partially.

The snapshots are mockups of the scenario I have and in reality I have over 200 folders with 50 text files each designated to dump the contents to respective named tables.

But the total number of rows I'm seeing after the package successfully executes is extremely low & can't be right. Is there anyway to get a count/list of the number of folders it traversed. Also, am I doing something wrong ?

Ideally, I'd just like to not have to start with .txt files but just have the whole thing go to folders, get the filenames ( which I think I've a working code for) & just dump all the info to the OLEDB Destinations.

Any help, link to resources is much appreciated.

1

There are 1 answers

17
Hadi On

Solution overview

You can achieving this using one DataFlow Task inside the foreach loop, but the trick is that you have to Read source flat file name and the Destination SQL Table name from variables

Note: Flat files structure must be the same and SQL Tables must have the same structure


Detailed Solution

  1. Right Click on the Control Flow window and click on Variables

enter image description here

  1. Declare 2 SSIS variables:

    • FlatFilename : of type String and assign a default value a random file path (i.e. C:\MockFolder\FileA.txt)
    • 'SQLTablename: of typeString` and assign to the following expression:

This is assuming that all destination tables has the same schema dbo

 "[dbo].[" + REPLACE(RIGHT( @[User::FlatFilename] , FINDSTRING(REVERSE( @[User::FlatFilename]  ) , "\\", 1) - 1),".txt","") + "]"

enter image description here

enter image description here

  1. Add a Foreach Loop Container and a DataFlow Task inside it, click on the DataFlow Task and on the properties Tab, Set the Delay Validation property to True

enter image description here

  1. Double Click on the Foreach Loop container and select the main Directory , and the files filter *.txt also choose the fully qualified retrieve file name option

enter image description here

  1. Go To variable mapping Tab and choose the @[User::FlatFilename] variable

enter image description here

  1. Add 2 Connection manager

    • FlatFileConnection: a Flat File connection manager, and configure it by selecting randomly a File (i.e. C:\MockFolder\FileA.txt)
    • OLEDBConnection: an OLEDB connection manager, and configure it to your Destination SQL Server Database
  2. In The DataFlow Task, add a Flat File Source and an OLEDB Destination, in the OLEDB Destination select Table name from variable option and select @[User::SQLTablename] as the variable name

enter image description here

  1. Map columns between source and Destination

  2. Click on the FlatFileConnection in the connection manager windows, press F4 to show the properties Tab, click on Expressions

enter image description here

  1. Select the Connection String property assign to it the following expression:

    @[user::FlatFilename]
    

enter image description here