Import multiple flat files to multiple SQL Tables

谁说胖子不能爱 提交于 2019-12-23 23:23:18

问题


Here's my folder setup.

Here's the File setup

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.

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.

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

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:


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

  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","") + "]"

  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

  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

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

  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

  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

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

    @[user::FlatFilename]
    



来源:https://stackoverflow.com/questions/47778881/import-multiple-flat-files-to-multiple-sql-tables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!