如何使用 SSIS 加载 EDI 823 密码箱文件中找到的父子数据?

发布于 2024-11-09 19:15:44 字数 70 浏览 3 评论 0 原文

我必须使用 ssis 将 EDI 823 密码箱文件加载到 SQl 服务器数据库中。谁能告诉我如何处理不同线路之间的父子关系。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

高冷爸爸 2024-11-16 19:15:44

更新:以下答案仅显示将简单父子数据加载到SQL Server中的示例。

检查此链接,了解如何使用 SSIS 加载实际的 EDI 823 密码箱 文件。

这是一个可能的选项,您可以使用它来加载父子/标题详细信息。

在这种情况下,我有一个平面文件,其中包含与国家/地区和州相关的标题详细信息。以 HDR 开头的行表示标题行,以 DTL 开头的行表示详细信息。有关示例文件内容,请参阅屏幕截图 #1

分步过程

  1. 在数据库(我选择 SQL Server)中,创建两个名为 dbo.Headerdbo.Detail 的表。有关这些表的创建脚本,请参阅部分。我们将借助此示例 SSIS 包读取平面文件内容来填充这些表。
  2. 在 SSIS 包上,拖放两个数据流任务。将它们命名为 HeaderDetail。请参阅屏幕截图 #2 以了解应如何放置它们。
  3. 创建一个名为 HeaderCode 的数据类型字符串变量。将值 HDR 分配给该变量。请参阅屏幕截图#3
  4. 配置标头数据流任务,如屏幕截图 #4 所示。下面的步骤 5 - 11 描述了 Header 数据流任务中的每个转换任务。
  5. Read File 是一个平面文件源,它被配置为读取屏幕截图#1 中显示的文件。此任务中使用的平面文件连接的配置设置如屏幕截图所示 #5 - #9
  6. 数据清理 是派生列转换任务并用于删除输入中存在的任何空格。请参阅屏幕截图#10
  7. 隔离数据是一个脚本组件转换任务。当您拖放脚本组件时,选择转换选项。请参阅屏幕截图 #11 以了解如何配置此任务的输入列。请参阅屏幕截图 #12 以了解如何配置此任务的输入和输出。输出列 IsHeader 的数据类型为 DT_BOOL,输出列 HeaderKey 的数据类型为 DT_STR,长度为 50。选择左侧的脚本,然后单击编辑脚本...。将脚本组件中的代码替换为脚本任务组件中使用的代码部分下给出的代码。
  8. Multicast顾名思义就是多播转换任务。它没有任何特殊配置。
  9. Fetch Detail 是一个条件分割转换任务。请参阅屏幕截图 #13 配置此任务。
  10. Header 是一个 OLE DB 目标,配置为连接到新创建的表 dbo.Header。此任务的字段映射如屏幕截图#14 所示。
  11. Staging 是一个配置为写入 CSV 文件的平面文件目标。该连接管理器的配置如屏幕截图#15 - #16所示。暂存连接管理器中有四列。 CodeValue 列的数据类型为 DT_STR,长度为 255IsHeader 的数据类型为 DT_BOOLHeaderKey 是长度为 50 的字符串数据类型。 Staging 任务的字段映射如屏幕截图 #17 所示。
  12. 然后我们继续执行下一个数据流任务Detail
  13. 配置详细数据流任务,如屏幕截图 #18 所示。下面的步骤 14 - 16 描述了详细数据流任务中的每个转换任务。
  14. Staging 是一个平面文件源,配置为使用在步骤 #11 中创建的 Staging 连接管理器。
  15. 获取标头 Id 是一个配置为指向表 dbo.Header 的查找转换。此任务的列配置如屏幕截图#19所示。
  16. Detail 是一个 OLE DB 目标,配置为连接到新创建的表 dbo.Detail。此任务的字段映射如屏幕截图 #20 所示。
  17. 屏幕截图 #21 和 #22 显示数据流任务 Header 和 Detail 的执行情况。
  18. 屏幕截图 #23 显示加载到表中的数据。

希望有帮助。

脚本任务组件中使用的代码 (在上面的步骤 #7 中提到)

只能在 C# 代码>SSIS 2008 及更高版本

/*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();
    }

}

表格:

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

屏幕截图 #1:

1

屏幕截图 #2:

2

屏幕截图 #3:

2

屏幕截图 #4:

23

屏幕截图 #5 :

3

屏幕截图#6:

4

屏幕截图 #7:

5

屏幕截图 #8:

6

屏幕截图 #9:

< img src="https://i.sstatic.net/niykv.png" alt="7">

屏幕截图 #10:

8

屏幕截图 #11:

9

屏幕截图#12:

10

屏幕截图 #13:

11

屏幕截图 #14:

12

屏幕截图 #15:

13

屏幕截图 #16: >

14

屏幕截图 #17:

15

屏幕截图 #18:

16

屏幕截图 #19:

17

屏幕截图 #20:

18

屏幕截图 #21:

19

屏幕截图 #22:

20

屏幕截图 #23:< /strong>

21

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文