问题
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:
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.Headeranddbo.Detail. Refer sectionTablesfor 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
HeaderandDetail. Refer screenshot #2 to see how they should be placed. - Create a variable of data type string named
HeaderCode. Assign the valueHDRto 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 Fileis 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 upis a derived column transformation task and is used to remove any spaces present in the input. Refer screenshot #10.Segregate datais 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 Columnson this task. Refer screenshot #12 to see how to configure theInput and Outputson this task. Output columnIsHeaderis of data type DT_BOOL and output column HeaderKey is of data type DT_STR with length 50. SelectScripton 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.Multicastas the name suggests is a multicast transformation task. It doesn't have any special configuration.Fetch Detailis a conditional split transformation task. Refer screenshot #13 to configure this task.Headeris 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.Stagingis 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.CodeandValuecolumns are of data type DT_STR with length 255;IsHeaderis of data type DT_BOOL;HeaderKeyis of data type string with length 50. Field mappings forStagingtask 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.
Stagingis a flat file source configured to use theStagingconnection manager created in step #11.Get Header Idis a lookup transformation configured to point to the table dbo.Header. Column configuration of this task is shown in screenshot #19.Detailis 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:
来源:https://stackoverflow.com/questions/6143433/how-to-load-parent-child-data-found-in-edi-823-lockbox-file-using-ssis