SSIS Foreach 循环项枚举器

Foreach 循环容器项枚举器会遍历容器列中提到的各项。本文档介绍了使用示例配置 SSIS Foreach 循环项枚举器的步骤。

我们使用下面的查询来演示此 SSIS Foreach 循环项枚举器。它返回所有可用国家/地区的 Fact Internet Sales 信息。我们将这些表数据导出到不同的文本文件中,文件名基于国家/地区名称。例如,英语国家/地区名称 = Australia 将加载到 Australia Sales CSV 文件中。

SELECT [EnglishCountryRegionName],[StateProvinceName],[City],[PostalCode]
,Fact.[UnitPrice],Fact.[ProductStandardCost],Fact.[TotalProductCost]
,Fact.OrderQuantity,Fact.[SalesAmount],Fact.[TaxAmt]
FROM [DimGeography] INNER JOIN DimCustomer ON
DimGeography.GeographyKey = DimCustomer.GeographyKey
INNER JOIN [FactInternetSales] AS Fact ON DimCustomer.CustomerKey = Fact.CustomerKey
ORDER BY EnglishCountryRegionName

请使用下面的查询查看不同的国家/地区名称列表。它们是澳大利亚、加拿大、法国、德国、英国和美国。

SELECT DISTINCT [EnglishCountryRegionName]
FROM [DimGeography]

要加载数据的文件列表。您可以看到,它们都是空的。

Empty CSV Files

SSIS Foreach 循环项枚举器示例

将 SSIS Foreach 循环容器拖放到控制流区域,以使用项枚举器。

SSIS ForEach Loop Item Enumerator 4

双击它将打开编辑器。将 SSIS 枚举器类型更改为 Foreach Item Enumerator。接下来,单击“Columns”按钮打开“For each Item Columns”窗口。“Add”按钮用于添加列和更改数据类型。对于其余的 LoopIetartions,请使用下面的链接。

  1. ADO 枚举器
  2. ADO.NET 架构行集枚举器
  3. 文件枚举器
  4. NodeList 枚举器
  5. SMO 枚举器
  6. 变量枚举器
Change Type to SSIS Foreach Item Enumerator and For each Item Columns window

我将把这些国家/地区名称添加为列值或要迭代的项。

Add Countries to Column Values

在 SSIS Foreach Loop Item Enumerator 变量映射选项卡下,单击“”。如果您已经有一个,请选择它。否则,使用下面的方法创建一个字符串类型的变量来保存国家/地区名称。

Create a new variable and SSIS ForEach Loop Item Enumerator Variable Mapping Tab

将数据流任务添加到 SSIS Foreach 循环项枚举器容器。

Add Data Flow Task to SSIS ForEach Loop Item Enumerator

双击打开 SSIS 数据流区域。然后,拖动 OLE DB Source 并双击它以打开编辑器。接下来,单击“New”按钮配置 OLE DB 连接管理器。我们选择了现有的连接管理器,但您也可以通过单击“New”按钮创建一个新的连接。

Use OLEDB Source configure the OLE DB Connection Manager

接下来,我们使用下面的查询和 WHERE 条件。这里的 ? 代表参数值。首先,将“Data Access Mode”选项更改为 SQL Command 并编写查询。

SELECT [EnglishCountryRegionName],[StateProvinceName],[City],[PostalCode]
,Fact.[UnitPrice],Fact.[ProductStandardCost],Fact.[TotalProductCost]
,Fact.OrderQuantity,Fact.[SalesAmount],Fact.[TaxAmt]
FROM [DimGeography] INNER JOIN DimCustomer ON
DimGeography.GeographyKey = DimCustomer.GeographyKey
INNER JOIN [FactInternetSales] AS Fact ON DimCustomer.CustomerKey = Fact.CustomerKey
WHERE EnglishCountryRegionName = ?
ORDER BY EnglishCountryRegionName
Sql Command

接下来,单击“Parameters”按钮并分配 SSIS Foreach Loop Item Enumerator 国家/地区变量。

Assign Parameter to Variable

拖动平面文件目标,并将 OLE DB 源 连接到它。接下来,双击 平面文件目标 打开编辑器,然后单击“New”按钮选择“Delimited”作为平面文件格式。

Add Flat File Destination for SSIS ForEach Loop Item Enumerator

单击“Browse”按钮选择任何一个文件来配置基本设置。

Select the File

接下来,勾选“第一数据行包含列名”选项以复制标题。

Checkmark the Column Names in the first data row

在“Flat File Destination Editor”中,转到“Mappings”选项卡以检查输入和可用目标列映射。

Check the input and available destination column mapping

连接字符串属性

选择 平面文件连接管理器,然后转到“Properties”窗口。接下来,单击“Expressions”旁边的浏览按钮。

选择 ConnectionString 作为 Property。单击表达式并编写表达式。这里,我们复制了实际的文件夹路径 + 国家/地区变量 + Sales.csv。Integration Services 对单个正斜杠会报错。所以,使用转义字符。

Within the Flat File Connection manager Properties - Write Expression for Flat File ConnectionString

运行 SSIS Foreach Loop Item Enumerator 包。

Run the SSIS Foreach Loop Item Enumerator package

现在您可以看到每个文件都有相应的国家/地区销售数据。

SSIS Foreach Loop Item Enumerator Result