Click here to Skip to main content
15,891,033 members
Articles / Programming Languages / SQL
Tip/Trick

Import a Flat file with Header and Detail Rows in SSIS

Rate me:
Please Sign up or sign in to vote.
4.93/5 (7 votes)
17 Dec 2015CPOL1 min read 23.9K   4   1
Import a Flat file with Header and Detail Rows in SSIS

Introduction

In this tip, I am going to demonstrate a step by step example of how to import a flat file which contains both Header and Detail Records with different formats in the SQL Server Database header & detail tables respectively using SSIS.

Using the Code

Prerequisite

  • An input file (Input.txt) with header and detail records
  • Header & Detail Tables in the database

Content of Flat file (Input.txt)

H1100
D1050P1
D1050P2
H2200
D2100P3
D2100P4

Flat file Header/Detail Record Specs

Image 1

Script for Header & Detail Tables

SQL
CREATE TABLE [dbo].[Header](
    [id] [varchar](50) NOT NULL,
    [amount] [int] NOT NULL
)

CREATE TABLE [dbo].[Detail](
    [id] [varchar](50) NOT NULL,
    [amount] [int] NOT NULL,
    [product] [char](100) NOT NULL
)

Step 1: Add a Data Flow Task to Control Flow.

Image 2

Step 2: Add a Flat File Source to the Data Flow.

Image 3

Step 3: Configure Flat File source.

  • Select file name (Input.txt)
  • Set Format as "Ragged Right"

Image 4

Step 4: Add Script Component from Data Flow Transformations and connect Flat File Source to it

Image 5

Step 5: Double click on Script component and Configure the Input Columns like below.

Image 6

Step 6: Configure Output under Inputs and Outputs tab like below as per Header & Details specifications.

Image 7

Step 7: Click on Edit Script and write the following code in the script window under Input0_ProcessInputRow method.

C#
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
    if (Row.Line.Substring(0, 1) == "H")
    {
        HeaderBuffer.AddRow();       
        HeaderBuffer.id = Row.Line.Substring(1, 1);
        HeaderBuffer.amount = Convert.ToInt16(Row.Line.Substring(2, 3));
    }
    else if (Row.Line.Substring(0, 1) == "D")
    {
        DetailBuffer.AddRow();
        DetailBuffer.id = Row.Line.Substring(1, 1);
        DetailBuffer.amount = Convert.ToInt16(Row.Line.Substring(2, 3));
        DetailBuffer.product = Row.Line.Substring(5, 2);
    }
}

Step 8: Add two OLE DB destination for header and detail and connect Header and Detail output of Script Component to each destination.

Image 8

Step 9: Configure OLE DB destination for Header & Detail like below:

Image 9

Image 10

Step 10: Now let's execute the package.

Image 11

Step 11: Package is executed successfully. Now let's check for the data in the tables.

SQL
select * from Header
select * from Detail

OUTPUT

Image 12

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionThe script component is throwing error in Input0Buffer class Pin
Jk1911-Jul-18 5:31
Jk1911-Jul-18 5:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.