Home > SSIS > SSIS vs Text File Importing – III

SSIS vs Text File Importing – III

In my previous posts << SSIS vs Text File Importing –I >> and << SSIS vs Text File Importing –II >> , we discussed about the Row Delimiter issue of SSIS while dealing with text files and the workaround for the same. In this post I will show you how to achieve the same through SSIS package.

Step1:  Open BIDS (Business Intelligence Development Studio), and create a solution for Integration Services.

Step2: Drag a foreach Loop Container task from the toolbox, to the package and rename it to some meaningful words.

Iterate for each text file

Step3: Double click the Foreach Loop container, and Click on “Collection” at the left pane.

Step4: Select “Foreach File Enumerator” from the Enumerator list and configure the values as shown below.

Foreach Loop Editor

Step 5: Click on the “Variable Mappings” and create a string variable to store intermediate text file path for each iteration of the loop. Assign the default value to one of the text files to avoid any validation error.

Add Variable

Step 6: Click OK and drag one “script task” inside the “foreach loop container”.

Step 7: Double click the “Script Task” and select “Microsoft Visual Basic 2008” under “Script Language” text box.

Step 8: Click on the ellipse button beside “ReadOnlyVariables” and check the variable named “User::TextFilePath”.

Select Variables

Step 9: Click on the “Edit Script …” button and paste the below code inside the Main function.

Expand image Script Task Code in VB .Net Expand

The above code is written in VB which will open the text file in Excel Application and will save it. This in turn reformats the missing tabs in the text file.




Step 10: Save the file and close it. Press OK.

Step 11: Drag a data flow task inside the “Foreach Loop Container” and double click it.

Step 12: Create source and destination connection manager for data transformation.

Step 13: Drag Flat file source for Text file and OLEDB destination for SQL table and configure it for a transformation to occur.

DataFlow Task

Step 13: Right click on the text file source connection manager, and click on “Properties”. On the “Properties” pane click on the ellipse button beside “Expressions”.

Step 14: On the “Property Expressions Editor”, select “Connection String” under the “Property” drop down box and click on ellipse button under “Expression”.

Property Expressions Editor

Step 15: On the “Expression Builder” dialog box, drag the variable “User::TextFilePath” inside “Expression:” text box and click on “Evaluate Expression” button. Click OK.

Expression Builder

Step 16: The overall package should look like below:

Iterate for each text file

Now run the package. After it got completed successfully, check the data in the destination table.

Use the below query:

 
SELECT [CarCompany]
      ,[CarBodyType]
      ,[CarName]
      ,[EngineType]
      ,[EMI (INR)]
      ,[LoanType]
      ,[Interest Rate]
  FROM [dbo].[Car_Info]

Output:

Output

We got exactly 9 rows and the data is properly aligned.

Hence the problem got resolved by adding a simple script task code that reformats the text file before loading it into database.

Note: Download the Full SSIS solution from here for FREE.  SSIS_RowDelimiter




Categories: SSIS
  1. dave
    May 18th, 2010 at 08:15 | #1

    what if the input file is not CSV but it is ! separated ?

  2. Jane
    July 14th, 2010 at 23:23 | #2

    Thank you arun, this is an excellent solution. I’m new to all this but really appreciate the idea to solve the delimiter issue.

  3. rajashekar
    August 7th, 2010 at 19:08 | #3

    I had followed same solution given by you. My requirement is that flat file source task should give error if then file is empty or all the rows are does not contain required no of mandatory column data

  4. August 7th, 2010 at 20:22 | #4

    well rajashekar,
    in that case go for script transformation task, it will be much easier.
    Kindly, raise your issues or problems in the FORUM area. You will definitely get some solution from our active users.
    FORUMS:
    http://www.sqllion.com/forums/

  5. taleleuma
    December 9th, 2010 at 00:09 | #5

    What if this solution needs to be deployed on Production and the Server does not have excel installed? Will EXCEL component be stable, if there are 1000+ files being processed within an hour with requirement of Zero failures?

  6. December 9th, 2010 at 07:25 | #6

    Hi Taleleuma,
    Excel component i.e. excel installation is needed if you going through this method.
    If your deploying server doesn’t have EXCEL, then you need to rely on “Source Script Component” (one of the transformation) where it can be handled explicitly through coding. And then its totally depends on the logic that is used to parse the rows and columns, to know whether it is able to process 1000+ files in one hour or not.

    –Arun

  7. Mark
    December 14th, 2011 at 10:24 | #7

    Thank you Sqllion!!!!

    Excel uploaded the CSV file correctly but not in SSIS. I have been trying to figure out how to solve this and it’s been three days… Almost killed my self.

    With your solution, the whole process can be automatic now from FTP to generating a report.

    Thank you Thank you Thank you….

  8. Mark
    December 23rd, 2011 at 05:37 | #8

    Hi Sqllion..

    Well, I still have problems loading CSV file.. I have been trying to search the solution but couldn’t.

    You have used the script to open and save CSV file. Is there any way to delete few columns and save as excel file?

    I have been looking for the way to do it for 3 days now… can’t find it.

    Merry Christmas~

    - Mark

  1. No trackbacks yet.