http://reddymsbitools.blogspot.com

Friday, 22 July 2011

How to Load the pipe delimited flat files into SQL Server ?

Source file:

0|1|2
1|2|3|4|"5|6|7"
1|2|3|4|5
a|b|c|d|"e|f|g"

If we need output below.

column1  ,column2,column3,column4,column5
0,1,2,null,null
1,2,3,4,567
1,2,3,4,5
a,b,c,d,efg

step1:
take the flatfile as a source
step2:
take script component tranformation and add the below code

' Microsoft SQL Server Integration Services user script component
' This is your new script component in Microsoft Visual Basic .NET
' ScriptMain is the entrypoint class for script components

Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Pipeline.Wrapper
Imports Microsoft.SqlServer.Dts.Runtime.Wrapper

_
_
Public Class ScriptMain
    Inherits UserComponent

    Public Overrides Sub Input0_ProcessInputRow(ByVal Row As Input0Buffer)
        '
        Dim Column5 As String
        Dim st As Char
        Dim File As String()
        Dim Delimiter As Char
        Delimiter = CChar("|")
        File = Row.Column0.Split(Delimiter)
        If (File.Length > 0) Then
            Row.Col1 = File(0)
        End If
        If (File.Length > 1) Then
            Row.Col2 = File(1)
        End If
        If (File.Length > 2) Then
            Row.Col3 = File(2)
        End If
        If (File.Length > 3) Then
            Row.Col4 = File(3)
        End If
        Dim int As Integer
        int = Row.Column0.IndexOf("""")
        'MsgBox(int)
        If (int < 0) Then
            Column5 = ""
        Else
            Column5 = Row.Column0.Substring(int, Row.Column0.Length - int)
        End If
        Column5 = Column5.Replace("""", "")

        Row.Col5 = Column5.Replace("|", " ")

        ' Add your code here
        '
    End Sub

End Class

step3:
load the data into sql server table

No comments:

Post a Comment