I have to load a flat file that has different header and detail with variable number of columns. These have parent child relations. How to load the data into SQL Server? The file looks like this:
DEP*0116960*20110511***01*061000104*DA*1000022220940
AMT*3*13006.05
QTY*41*3
QTY*42*5
BAT*20110511**STAWRRY11051101
AMT*2*9332.33
QTY*42*2
BPR*I*4799*C*CHK*PBC*01*011500010*DA*394001464351
REF*CK*0000001002
BPR*I*4533.33*C*CHK*PBC*01*011500010*DA*394001464351
REF*CK*0000001001
BAT*20110511**STAWRRY11051102
AMT*2*1986.99
QTY*42*2
BPR*I*853.97*C*CHK*PBC*01*111000614*DA*708340062
REF*CK*0010736416
RMR*ST*00090183**853.97*12199.61
REF*BE*01*0123181825*0
REF*SL*NA*191219012318
DTM*810*20110430
BPR*I*1133.02*C*CHK*PBC*01*111000614*DA*708340062
REF*CK*0010736417
RMR*ST*00090184**1133.02*16186.04
REF*BE*01*0123181825*0
REF*SL*NA*191219012318
DTM*810*20110430
BAT*20110511**STAWRRY11051103
AMT*2*1686.73
QTY*42*1
BPR*I*1686.73*C*CHK*PBC*01*075911742*DA*0100461755
REF*CK*0000002795
RMR*ST*00094075**1686.73*42168.16
REF*BE*01*0123181825*0
REF*SL*NA*191219012318
DTM*810*20110331
SE*39*000000088
GE*1*88
IEA*1*000000088
You can use a script component source to build the rules on handling the file. That's well described here and here. You may be able to use the EDI Source from CozyRoc, but I don't believe they have indicated it has been tested with EDI 823.
Here is one possible way of loading this file into SQL Server. Below shown example reads the contents of EDI 823 Lockbox file and loads into multiple tables along with the relationship. I am sure that there are other better ways of doing this. This is just one example of loading an EDI file into SQL Server.
The example reads the EDI file line by line and then splits them based on the character asterisk (*). The script component assigns the value to the variables in the Script Component to populate the tables. After the data is populated by the
Data Flow Task
, theExecute SQL Task
will update the ParentId column in the tablesdbo.AMT
,dbo.DTM
,dbo.QTY
anddbo.REF
using the stored proceduredbo.UpdateHierarchy
. The table 'ISA' contains the data of all other segments. Screenshots show how the data is stored in the child tables.SetId
columns used in all the tables will group data of a file in order to avoid wrong parent id being referred from data pulled from another file.SetId
will be unique for each file loaded into these tables.Step-by-step process:
Create 5 tables named
dbo.AMT
,dbo.DTM
,dbo.ISA
,dbo.QTY
anddbo.REF
and a stored procedure nameddbo.UpdateHierarchy
in the database using the scripts provided under SQL Scripts section. TablesAMT
,DTM
,QTY
andREF
will store the data of the segments named similarly and the tableISA
will contain all other segment data.Create an
OLE DB Connection
named SQLServer to connect to the SQL Server instance and create aFlat File Connection
namedSource
as shown in screenshots #""1"" - #4. Flat File connection will use the EDI file. Remove theColumn delimiter
because the file has varying number of elements. This example will split the elements using theScript Component
.On the SSIS package, create 5 variables as shown in screenshot #5. Also, place a
Data Flow task
and anExecute SQL Task
on theControl Flow
tab of the package as shown in screenshot #5.Configure the Data Flow Task with
Flat File Source
,Script Component
,Multicast
,Conditional Split
and 5OLE DB Destinations
as shown in screenshot #6.Configure the
Flat File Source
to read the EDI file using the Flat File connection nameSource
.Configure the
Script Component
as Transformation task as shown in screenshots #7 and #8. Create all the other variables using the names and data types using the data provided under Script Component Variables section. Replace the class ScriptMain present within theScript Component Transformation
task with the code provided under Script Component Code.Configure the
Conditional Split
as shown in screenshot #9.Configure the
OLE DB Destination
AMT to insert data into the tabledbo.AMT
and map fields as shown in screenshot #10.Configure the
OLE DB Destination
QTY to insert data into the tabledbo.QTY
and map fields as shown in screenshot #11.Configure the
OLE DB Destination
REF to insert data into the tabledbo.REF
and map fields as shown in screenshot #12.Configure the
OLE DB Destination
DTM to insert data into the tabledbo.DTM
and map fields as shown in screenshot #13.Configure the
OLE DB Destination
Other to insert data into the tabledbo.ISA
and map fields as shown in screenshot #14.On the Data Flow tab, configure the
Execute SQL Task
as shown in screenshot #15.Screenshots #16 and #17 shows sample package execution. File data used for testing this example is provided under section
EDI File Content
.Screenshot #18 shows data in table
dbo.AMT
in comparison with data in tabledbo.ISA
.Screenshot #19 shows data in table
dbo.QTY
in comparison with data in tabledbo.ISA
.Screenshot #20 shows data in table
dbo.REF
in comparison with data in tabledbo.ISA
.Screenshot #21 shows data in table
dbo.DTM
in comparison with data in tabledbo.ISA
.Hope that helps.
SQL Scripts:
Script Component Variables:
Script Component Code:
VB.NET code that can be used in
SSIS 2005 and above
.EDI File Content:
Screenshot #1:
Screenshot #2:
Screenshot #3:
Screenshot #4:
Screenshot #5:
Screenshot #6:
Screenshot #7:
Screenshot #8:
Screenshot #9:
Screenshot #10:
Screenshot #11:
Screenshot #12:
Screenshot #13:
Screenshot #14:
Screenshot #15:
Screenshot #16:
Screenshot #17:
Screenshot #18:
Screenshot #19:
Screenshot #20:
Screenshot #21:
This is why translation engines are available and so widely used. I love the detail of the solution above, but also feel like there's some reinvention of the wheel.
Take a look at Jitterbit. It is open source, and pretty user friendly for data transformations.
You can model your source, model your target and then do a transformation. Based on your models, you'll have much more control over the transformation that what SSIS will give you. Sure there's a learning curve, but you can automate the communication / transformation. There also would be ZERO code to write.