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.
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:
dbo.Header
and dbo.Detail
. Refer section Tables
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.Header
and Detail
. Refer screenshot #2 to see how they should be placed.HeaderCode
. Assign the value HDR
to the variable. Refer screenshot #3.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 the Input Columns
on this task. Refer screenshot #12 to see how to configure the Input and Outputs
on this task. Output column IsHeader
is of data type DT_BOOL and output column HeaderKey is of data type DT_STR with length 50. Select Script
on the left side and click on the Edit 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
and Value
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 for Staging
task are shown in screenshot #17.Detail
.Staging
is a flat file source configured to use the Staging
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.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: