I have to load EDI 823 lock box file into SQl server database using ssis. Can anyone tell me how to do the parent child relationship between different lines.
1 Answers
UPDATE: Following answer shows only an example of loading a simple parent-child data into SQL Server.
Check this link to see how to load an actual EDI 823 lockbox
file using SSIS.
Here is a possible option that you could use to load parent-child/header-detail information.
In this scenario, I have a flat file containing header-detail information pertaining to countries and states. Rows beginning with HDR
denote header line and rows beginning with DTL
denote detail. Refer screenshot #1 for sample file content.
Step-by-step process:
- In a database (I chose SQL Server), create two tables named
dbo.Header
anddbo.Detail
. Refer sectionTables
for the create scripts of these tables. We will be populating these tables by reading the flat file content with the help of this sample SSIS package. - On the SSIS package, drag and drop two data flow tasks. Name them as
Header
andDetail
. Refer screenshot #2 to see how they should be placed. - Create a variable of data type string named
HeaderCode
. Assign the valueHDR
to the variable. Refer screenshot #3. - Configure the Header data flow task as shown in screenshot #4. Below steps 5 - 11 describe each of the transformation task inside Header data flow task.
Read File
is a flat file source and it is configured to read the file shown in the screenshot #1. Configuration settings of the flat file connection used in this task are shown in screenshots #5 - #9Data clean up
is a derived column transformation task and is used to remove any spaces present in the input. Refer screenshot #10.Segregate data
is a script component transformation task. When you drag and drop the script component, select Transformation option. Refer screenshot #11 to see how to configure theInput Columns
on this task. Refer screenshot #12 to see how to configure theInput and Outputs
on this task. Output columnIsHeader
is of data type DT_BOOL and output column HeaderKey is of data type DT_STR with length 50. SelectScript
on the left side and click on theEdit Script...
. Replace the code in script component with the code given under the Code used in the script task component section.Multicast
as the name suggests is a multicast transformation task. It doesn't have any special configuration.Fetch Detail
is a conditional split transformation task. Refer screenshot #13 to configure this task.Header
is a OLE DB destination configured to connect to the newly created table dbo.Header. Field mappings for this task are shown in the screenshot #14.Staging
is a flat file destination configured to write to a CSV file. Configuration of this connection manager are shown in screenshots #15 - #16. There are four columns in the Staging connection manager.Code
andValue
columns are of data type DT_STR with length 255;IsHeader
is of data type DT_BOOL;HeaderKey
is of data type string with length 50. Field mappings forStaging
task are shown in screenshot #17.- We then move on to the next data flow task
Detail
. - Configure the Detail data flow task as shown in screenshot #18. Below steps 14 - 16 describe each of the transformation task inside Detail data flow task.
Staging
is a flat file source configured to use theStaging
connection manager created in step #11.Get Header Id
is a lookup transformation configured to point to the table dbo.Header. Column configuration of this task is shown in screenshot #19.Detail
is a OLE DB destination configured to connect to the newly created table dbo.Detail. Field mappings for this task are shown in the screenshot #20.- Screenshots #21 and #22 display the execution of the data flow tasks Header and Detail.
- Screenshot #23 displays the data loaded into the table.
Hope that helps.
Code used in the script task component (Mentioned in step #7 above):
C# code that can be used only in SSIS 2008 and above
.
.
/*Microsoft SQL Server Integration Services Script Component
* Write scripts using Microsoft Visual C# 2008.
* ScriptMain is the entry point class of the script.*/
using System;
using System.Data;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;
using Microsoft.SqlServer.Dts.Runtime.Wrapper;
[Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]
public class ScriptMain : UserComponent
{
string currentValue = string.Empty;
string previousHeader = string.Empty;
public override void PreExecute()
{
base.PreExecute();
}
public override void PostExecute()
{
base.PostExecute();
}
public override void Input0_ProcessInputRow(Input0Buffer Row)
{
IDTSVariables100 varCollection = null;
this.VariableDispenser.LockForRead("User::HeaderCode");
this.VariableDispenser.GetVariables(out varCollection);
currentValue = Row.ValueTrimmed.ToString();
if (Row.CodeTrimmed.ToString() == varCollection["User::HeaderCode"].Value.ToString())
{
Row.IsHeader = true;
if (previousHeader != currentValue)
{
previousHeader = currentValue;
}
}
Row.HeaderKey = previousHeader;
varCollection.Unlock();
}
}
Tables: .
CREATE TABLE [dbo].[Detail](
[DetailId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](255) NOT NULL,
[HeaderId] [int] NOT NULL,
CONSTRAINT [PK_Detail] PRIMARY KEY CLUSTERED
(
[DetailId] ASC
)) ON [PRIMARY]
GO
CREATE TABLE [dbo].[Header](
[HeaderId] [int] IDENTITY(1,1) NOT NULL,
[Name] [varchar](255) NOT NULL,
CONSTRAINT [PK_Header] PRIMARY KEY CLUSTERED
(
[HeaderId] ASC
)) ON [PRIMARY]
GO
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:
Screenshot #22:
Screenshot #23:

- 1
- 1
-
@ Shiva- I noticed that the file that I have had different number of columns. i.e all the rows in file has different number of columns. Can you tell me how can I use your code for this type of file. Thanks! – Prateek Jun 02 '11 at 22:03
-
@ Shiva *The problem is that the file that I have, has the rows with variable number of columns. So I could not figure how to use the code that you have sent. Thanks. Please help * – Prateek Jun 03 '11 at 13:39
-
@ Shiva- This script in the script task is also not working. Also can you please tell me how to modify this script if there are multiple parent children – Prateek Jun 03 '11 at 17:02
-
@Shiva - I am using ssis 2005. – Prateek Jun 03 '11 at 18:55
-
@ Shiva- Can you paste the code to identify multiple headers and Detail. The file looks like this – Prateek Jun 03 '11 at 19:19
-
@Shiva- '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' – Prateek Jun 03 '11 at 19:20
-
@Shiva- Can you send me your email. I want to send you the file that I want to load. Thanks – Prateek Jun 03 '11 at 19:22