如何将多个输入分组到一个输出中

发布于 2024-11-15 09:33:42 字数 384 浏览 2 评论 0原文

我有一个像这样的 Excel

region    state    city
-------------------------
south     state1    city1
north     state2    city2

,我想填充这样的数据库表

  id      name       type
 ------------------------
   1      state1     state
   2      south      region
   3      city1      city

,但我不知道如何对这些列和标题进行分组以填充数据库表。有什么想法吗?我对这个应用程序很迷茫

I have an Excel like this

region    state    city
-------------------------
south     state1    city1
north     state2    city2

and I want to fill a data base table like this

  id      name       type
 ------------------------
   1      state1     state
   2      south      region
   3      city1      city

But I don't have any idea about how to group these columns and headers to fill the data base table. Any ideas? I'm pretty lost with this app

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

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

发布评论

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

评论(1

薄情伤 2024-11-22 09:33:42

您可以使用数据流任务中提供的Unpivot Transformation在SSIS中实现此目的。以下示例说明了如何做到这一点。该示例使用 SSIS 2008 R2SQL 2008 R2 数据库。

分步过程:

  1. 使用示例数据创建一个 Excel 文件,如屏幕截图 #1 所示。我已将该文件命名为 Source.xlsx

  2. 使用SQL 脚本部分中给出的脚本在 SQL Server 数据库中创建一个名为dbo.Destination 的表。该表将填充 Excel 数据。我引入了一个名为 GroupId 的新字段,以便可以将数据分组在一起。

  3. 在 SSIS 包上,创建一个名为 Excel 的 Excel 连接和一个名为 SQLServer 的 OLE DB 连接,如屏幕截图 #2 所示。 Excel 连接管理器 应按屏幕截图 #3 所示进行配置。 OLE DB 连接将配置为连接到您选择的数据库。

  4. 在包的控制流选项卡上,放置一个数据流任务,如屏幕截图#4所示。

  5. 使用 Excel 源脚本组件逆透视转换配置数据流选项卡,如屏幕截图 #5 所示code> 和 OLE DB 目标

  6. 配置 Excel 源,如屏幕截图 #6 和 #7 所示。这将从 Excel 文件中读取数据。

  7. 脚本组件配置为转换并添加输出列,如屏幕截图#8所示。在脚本部分,单击编辑脚本并将代码替换为脚本组件代码部分下给出的代码。

  8. 配置 Unpivot 转换,如屏幕截图 #9 所示。我们不想转换 GroupId,因此不要对其进行配置,而是将其设置为 Pass Through

  9. 配置 OLE DB 目标,如屏幕截图 #10 和 #11 所示。

  10. Screenshot #12 显示包执行之前dbo.Destination表中的数据。

  11. 屏幕截图 #13 显示包执行。

  12. Screenshot #14 显示包执行之后dbo.Destination表中的数据。

希望有帮助。

SQL 脚本:

CREATE TABLE [dbo].[Destination](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](255) NULL,
    [Type] [nvarchar](255) NULL,
    [GroupId] [int] NULL,
 CONSTRAINT [PK_Destination] PRIMARY KEY CLUSTERED ([Id] ASC)
) ON [PRIMARY]
GO

脚本组件代码:

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
{
    int groupId = 1;
    public override void PreExecute()
    {
        base.PreExecute();
    }

    public override void PostExecute()
    {
        base.PostExecute();
    }

    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        Row.GroupId = groupId;
        groupId += 1;
    }
}

屏幕截图 #1:

1

屏幕截图 #2:

2

屏幕截图 #3:

3

屏幕截图 #4:

4

屏幕截图 #5:

5

屏幕截图 #6:

6

屏幕截图 #7:

7

屏幕截图 #8:

8

屏幕截图#9:

9

屏幕截图 #10:

10

屏幕截图 #11:

11

屏幕截图 #12:

12

屏幕截图 #13:

13

屏幕截图 #14:< /strong>

14

You can achieve this in SSIS using Unpivot Transformation available in Data Flow Task. Following example illustrates how this can be done. The example uses SSIS 2008 R2 and SQL 2008 R2 database.

Step-by-step process:

  1. Create an Excel file with sample data as shown in screenshot #1. I have named the file as Source.xlsx.

  2. Create a table in the SQL Server database named dbo.Destination using the script given under SQL Scripts section. This table will be populated with Excel data. I have introduced a new field named GroupId so the data can be grouped together.

  3. On the SSIS package, create an Excel connection named Excel and an OLE DB connection named SQLServer as shown in screenshot #2. Excel connection manager should be configured as shown as in screenshot #3. OLE DB connection will be configured to connect to the database of your choice.

  4. On the package's Control Flow tab, place a Data Flow Task as shown in screenshot #4.

  5. Configure the Data Flow tab as shown in screenshot #5 with an Excel source, Script component, Unpivot transformation and an OLE DB destination.

  6. Configure the Excel Source as shown in screenshots #6 and #7. This will read the data from Excel file.

  7. Configure the Script Component as Transformation and add an Output column as shown in screenshot #8. On the Script section, click Edit Script and replace the code with the code given under Script Component Code section.

  8. Configure the Unpivot transformation as shown in screenshot #9. We don't want to transform the GroupId, so don't configure that but make it to Pass Through.

  9. Configure the OLE DB destination as shown in screenshots #10 and #11.

  10. Screenshot #12 shows data in the table dbo.Destination before the package execution.

  11. Screenshot #13 shows the package execution.

  12. Screenshot #14 shows data in the table dbo.Destination after the package execution.

Hope that helps.

SQL Scripts:

CREATE TABLE [dbo].[Destination](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [Name] [nvarchar](255) NULL,
    [Type] [nvarchar](255) NULL,
    [GroupId] [int] NULL,
 CONSTRAINT [PK_Destination] PRIMARY KEY CLUSTERED ([Id] ASC)
) ON [PRIMARY]
GO

Script Component Code:

C# code that can be used only in SSIS 2008 or 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
{
    int groupId = 1;
    public override void PreExecute()
    {
        base.PreExecute();
    }

    public override void PostExecute()
    {
        base.PostExecute();
    }

    public override void Input0_ProcessInputRow(Input0Buffer Row)
    {
        Row.GroupId = groupId;
        groupId += 1;
    }
}

Screenshot #1:

1

Screenshot #2:

2

Screenshot #3:

3

Screenshot #4:

4

Screenshot #5:

5

Screenshot #6:

6

Screenshot #7:

7

Screenshot #8:

8

Screenshot #9:

9

Screenshot #10:

10

Screenshot #11:

11

Screenshot #12:

12

Screenshot #13:

13

Screenshot #14:

14

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