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
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 Taskwill update the ParentId column in the tablesdbo.AMT,dbo.DTM,dbo.QTYanddbo.REFusing 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.SetIdcolumns 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.SetIdwill be unique for each file loaded into these tables.Step-by-step process:
Create 5 tables named
dbo.AMT,dbo.DTM,dbo.ISA,dbo.QTYanddbo.REFand a stored procedure nameddbo.UpdateHierarchyin the database using the scripts provided under SQL Scripts section. TablesAMT,DTM,QTYandREFwill store the data of the segments named similarly and the tableISAwill contain all other segment data.Create an
OLE DB Connectionnamed SQLServer to connect to the SQL Server instance and create aFlat File ConnectionnamedSourceas shown in screenshots #””1″” – #4. Flat File connection will use the EDI file. Remove theColumn delimiterbecause 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 taskand anExecute SQL Taskon theControl Flowtab of the package as shown in screenshot #5.Configure the Data Flow Task with
Flat File Source,Script Component,Multicast,Conditional Splitand 5OLE DB Destinationsas shown in screenshot #6.Configure the
Flat File Sourceto read the EDI file using the Flat File connection nameSource.Configure the
Script Componentas 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 Transformationtask with the code provided under Script Component Code.Configure the
Conditional Splitas shown in screenshot #9.Configure the
OLE DB DestinationAMT to insert data into the tabledbo.AMTand map fields as shown in screenshot #10.Configure the
OLE DB DestinationQTY to insert data into the tabledbo.QTYand map fields as shown in screenshot #11.Configure the
OLE DB DestinationREF to insert data into the tabledbo.REFand map fields as shown in screenshot #12.Configure the
OLE DB DestinationDTM to insert data into the tabledbo.DTMand map fields as shown in screenshot #13.Configure the
OLE DB DestinationOther to insert data into the tabledbo.ISAand map fields as shown in screenshot #14.On the Data Flow tab, configure the
Execute SQL Taskas 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.AMTin comparison with data in tabledbo.ISA.Screenshot #19 shows data in table
dbo.QTYin comparison with data in tabledbo.ISA.Screenshot #20 shows data in table
dbo.REFin comparison with data in tabledbo.ISA.Screenshot #21 shows data in table
dbo.DTMin 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: