Search code examples
ssisedix12

How to load parent child data found in EDI 823 lockbox file using SSIS?


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.


Solution

  • 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:

    1. In a database (I chose SQL Server), create two tables named 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.
    2. On the SSIS package, drag and drop two data flow tasks. Name them as Header and Detail. Refer screenshot #2 to see how they should be placed.
    3. Create a variable of data type string named HeaderCode. Assign the value HDR to the variable. Refer screenshot #3.
    4. 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.
    5. 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 - #9
    6. Data clean up is a derived column transformation task and is used to remove any spaces present in the input. Refer screenshot #10.
    7. 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.
    8. Multicast as the name suggests is a multicast transformation task. It doesn't have any special configuration.
    9. Fetch Detail is a conditional split transformation task. Refer screenshot #13 to configure this task.
    10. 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.
    11. 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.
    12. We then move on to the next data flow task Detail.
    13. 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.
    14. Staging is a flat file source configured to use the Staging connection manager created in step #11.
    15. 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.
    16. 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.
    17. Screenshots #21 and #22 display the execution of the data flow tasks Header and Detail.
    18. 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:

    1

    Screenshot #2:

    2

    Screenshot #3:

    2

    Screenshot #4:

    23

    Screenshot #5:

    3

    Screenshot #6:

    4

    Screenshot #7:

    5

    Screenshot #8:

    6

    Screenshot #9:

    7

    Screenshot #10:

    8

    Screenshot #11:

    9

    Screenshot #12:

    10

    Screenshot #13:

    11

    Screenshot #14:

    12

    Screenshot #15:

    13

    Screenshot #16:

    14

    Screenshot #17:

    15

    Screenshot #18:

    16

    Screenshot #19:

    17

    Screenshot #20:

    18

    Screenshot #21:

    19

    Screenshot #22:

    20

    Screenshot #23:

    21