ssis for循环容器_SSIS Foreach循环与For循环容器

ssis for循环容器

In this article, first, we will briefly describe foreach loops and for loops. Then, we will give an overview of SSIS Foreach Loop Container and For Loop Container. Finally, we will compare these two containers.

首先,在本文中,我们将简要描述foreach循环和for循环。 然后,我们将概述SSIS Foreach循环容器和For循环容器。 最后,我们将比较这两个容器。

This article is the tenth article in the SSIS feature face to face series, which aims to remove confusion and compare and contrast similar features provided by SQL Server Integration Services.

本文是SSIS功能面对面系列文章中的第十篇,目的是消除混乱并比较和对比SQL Server Integration Services提供的类似功能。

介绍 (Introduction)

In general, loops are used to execute one or more statements repeatedly until a specific condition is satisfied. They are one of the most basic statements in programming and are implemented in a wide range of programming languages such as Java, Python, Visual Basic, C#, SQL and others. In addition to programming languages, loop functions are implemented in data warehousing and data preparation technologies such as SQL Server Integration Services.

通常,循环用于重复执行一个或多个语句,直到满足特定条件为止。 它们是编程中最基本的语句之一,并以各种编程语言(例如Java,Python,Visual Basic,C#,SQL等)实现。 除了编程语言外,循环功能还通过数据仓库和数据准备技术(例如SQL Server Integration Services)实现。

There are different types of loops, such as:

有不同类型的循环,例如:

  • While

  • Do…While

    做...而
  • For

    对于
  • Foreach

    Foreach

In this section, we will briefly describe the for loop and foreach loop since this will make understanding SSIS Loop Containers easier.

在本节中,我们将简要描述for循环和foreach循环,因为这将使理解SSIS循环容器更加容易。

对于循环 (For Loop)

For loop is a counter-based loop that is composed of three main phases:

For循环是一个基于计数器的循环,由三个主要阶段组成:

  1. Initialization

    初始化
  2. Evaluation

    评价
  3. Counter increment

    计数器增量

In the first phase, an initial value is assigned to the counter. This value is used in the first evaluation phase. In the evaluation phase, if the evaluation condition result is false, the loop will end; if true, it will execute the statements within the loop, then increment the counter and go back to the evaluation phase. The following diagram shows the for loop flow:

在第一阶段,将初始值分配给计数器。 该值用于第一评估阶段。 在评估阶段,如果评估条件结果为假,则循环将结束;否则,循环将结束。 如果为true,它将在循环内执行语句,然后增加计数器并返回到评估阶段。 下图显示了for循环流:

ssis for循环容器_SSIS Foreach循环与For循环容器_第1张图片

As an example, the following code is a simple for loop clause written in C#. In this loop, the counter is initiated at 0, the evaluation phase is to check if the counter is less than 10, and after each loop the counter is incremented by 1:

例如,以下代码是用C#编写的简单的for循环子句。 在此循环中,计数器从0开始,评估阶段是检查计数器是否小于10,并且在每个循环之后,计数器将递增1:

for(int i=0;i<10;i++){
 
       // statement must be written here
 
  }

Foreach循环 (Foreach Loop)

A foreach loop is very similar to a for loop, except there is no evaluation phase. This is because you are looping over a collection of objects, and for each object in the collection, you execute a specific statement. As an example, you have a list of names and you need to loop over these names without the need for an evaluation phase or to use any counter (not index-based loop).

除了没有评估阶段外,foreach循环与for循环非常相似。 这是因为要遍历对象的集合,并且对集合中的每个对象执行特定的语句。 例如,您有一个名称列表,您需要遍历这些名称而无需评估阶段或使用任何计数器(不是基于索引的循环)。

Case in point, the following code is a foreach loop clause that loops over a string located in a list of strings in C#:

例如,以下代码是一个foreach循环子句,该子句循环遍历C#中的字符串列表中的字符串:

foreach(string str in list){
 
  // to do statement
 
  }
  • Note: For more information about for/foreach loops, refer to this article: 注意 :有关for / foreach循环的更多信息,请参见本文: w3schools.com – C# For Loopw3schools.com – C#For循环

SSIS循环容器 (SSIS Loop Containers)

After describing for loops and foreach loops, we will show how these operations are implemented within SSIS. There are many methods to implement loops within SSIS within different scopes:

在描述了for循环和foreach循环之后,我们将展示如何在SSIS中实现这些操作。 在不同范围内,有很多方法可以在SSIS中实现循环:

    • SSIS For Loop Container

      SSIS循环容器
    • SSIS Foreach Loop Container

      SSIS Foreach循环容器
    • Within a script task

      在脚本任务中
    • Within a script component

      在脚本组件中

In this section, we will not focus on how to implement loops using scripting in SSIS. Instead, we will give an overview of the containers mentioned above.

在本节中,我们将不专注于如何使用SSIS中的脚本来实现循环。 相反,我们将对上述容器进行概述。

SSIS循环容器 (SSIS For Loop Container)

SSIS For Loop Container has the same context of for loops, as we mentioned above. It has three phases: Initialization, Evaluation and Counter Increment. But, the statements to be executed within a For Loop Container in SSIS are in the form of SSIS tasks (Execute SQL Task, Script Task, File System Task, Execute Process Task, etc.):

如上所述,SSIS For循环容器具有与for循环相同的上下文。 它分为三个阶段:初始化,评估和反增量。 但是,要在SSIS中的For循环容器内执行的语句是以SSIS任务(执行SQL任务,脚本任务,文件系统任务,执行过程任务等)的形式:

ssis for循环容器_SSIS Foreach循环与For循环容器_第2张图片

When we open the SSIS For Loop Container editor, there are three main properties that we have to set:

当我们打开SSIS For循环容器编辑器时,必须设置三个主要属性:

  • InitExpression: What is the initialization expression? InitExpression:什么是初始化表达式?
  • EvalExpression: What is the evaluation expression? EvalExpression:评估表达式是什么?
  • AssignExpression: What is the counter incremental expression? AssignExpression:什么是计数器增量表达式?

Variables must be added within the SSIS package, and to use them, you must add the @ character as the prefix. As example, if we added the @[User::Counter] variable, to implement a for loop similar to For(int i=0;i<10;i++){},we must use the following expressions:

必须在SSIS包中添加变量,并且要使用它们,必须添加@字符作为前缀。 例如,如果我们添加@ [User :: Counter]变量,以实现类似于For(int i = 0; i <10; i ++){}的for循环,则必须使用以下表达式:

  • InitExpression: @Counter = 0 InitExpression: @Counter = 0
  • EvalExpression: @Counter<10 EvalExpression: @Counter <10
  • AssignExpression: @Counter = @Counter + 1 AssignExpression: @Counter = @Counter + 1

ssis for循环容器_SSIS Foreach循环与For循环容器_第3张图片
  • Note: To learn more about SSIS For Loop Container, refer to the following official documentation: 注意 :要了解有关SSIS For循环容器的更多信息,请参考以下官方文档: SSIS For Loop ContainerSSIS For循环容器

SSIS Foreach循环容器 (SSIS Foreach Loop Container)

The SSIS Foreach Loop Container is more complicated than the For Loop Container since it has many use cases and requires a more complex configuration:

SSIS Foreach循环容器比For循环容器更复杂,因为它具有许多用例,并且需要更复杂的配置:

ssis for循环容器_SSIS Foreach循环与For循环容器_第4张图片

There are different types of enumerators in the SSIS Foreach Loop Container. You can select the enumerator type from the collection tab within the SSIS Foreach Loop Container editor form:

SSIS Foreach循环容器中有不同类型的枚举器。 您可以从SSIS Foreach循环容器编辑器表单的“收集”选项卡中选择枚举器类型:

  • Foreach item enumerator: Loop over a set of items that can be defined manually within the SSIS Foreach Loop Container editor Foreach项目枚举器:循环可在SSIS Foreach循环容器编辑器中手动定义的一组项目
  • Foreach File enumerator: Loop over files within a specific directory Foreach文件枚举器:循环访问特定目录中的文件
  • Foreach ADO enumerator: Loop over file rows in an ADO Recordset Foreach ADO枚举器:循环遍历ADO Recordset中的文件行
  • Foreach ADO.NET Schema Rowset enumerator: Loop over schema information from a specific data source (tables in a database) Foreach ADO.NET架构行集枚举器:循环来自特定数据源(数据库中的表)的架构信息
  • Foreach from Variable enumerator: Loop over items stored within an SSIS variable of type object (must be enumerable) 从变量枚举器进行Foreach:循环存储在类型为object的SSIS变量中的项(必须可枚举)
  • Foreach NodeList enumerator: Loop over a result set of an XML Path Language (XPath) expression Foreach NodeList枚举器:遍历XML路径语言(XPath)表达式的结果集
  • Foreach SMO enumerator: Loop over SQL Server Management Objects (SMO) objects, such as available servers Foreach SMO枚举器:循环遍历SQL Server管理对象(SMO)对象,例如可用服务器
  • Foreach HDFS File enumerator: Loop over files located within a Hadoop distributed file system directory Foreach HDFS文件枚举器:循环访问Hadoop分布式文件系统目录中的文件
  • Foreach Azure Blob: Loop over blobs in a blob container in Azure Storage Foreach Azure Blob:循环遍历Azure存储中Blob容器中的Blob
  • Foreach ADLS File: Loop over files in a directory in Azure Data Lake Store Foreach ADLS文件:循环遍历Azure Data Lake Store中目录中的文件
  • Foreach Data Lake Storage Gen2 File: Loop over files in a directory in Azure Data Lake Store Gen2 Foreach Data Lake Storage Gen2文件:循环遍历Azure Data Lake Store Gen2目录中的文件

ssis for循环容器_SSIS Foreach循环与For循环容器_第5张图片

Each enumerator has its own properties that we must configure.

每个枚举器都有我们必须配置的自己的属性。

To catch the current item while looping over a collection, we must add an SSIS package and map this variable to the item within the SSIS Foreach Loop Container variable mappings tab page by specifying the item index within the current row and the variable name.

要在遍历集合时捕获当前项目,我们必须添加SSIS包并将此变量映射到SSIS Foreach循环容器变量映射选项卡页面中的项目,方法是在当前行和变量名称中指定项目索引。

ssis for循环容器_SSIS Foreach循环与For循环容器_第6张图片
  • Note: For more information about SSIS Foreach Loop Container, refer to the official documentation: 注意 :有关SSIS Foreach循环容器的更多信息,请参考官方文档: SSIS Foreach Loop ContainerSSIS Foreach循环容器

获取循环迭代号 (Getting the loop iteration number)

By using the SSIS Foreach Loop Container, we can get the current item using variable mappings, but we cannot know the iteration number. To do that, we must add an expression task or a script task with an incremental variable. For more information, refer to the following answer on Stackoverflow.com: Display foreach loop iteration number in SSIS

通过使用SSIS Foreach循环容器,我们可以使用变量映射获取当前项,但是我们不知道迭代次数。 为此,我们必须添加带有增量变量的表达式任务或脚本任务。 有关更多信息,请参考Stackoverflow.com上的以下答案: 在SSIS中显示foreach循环迭代号

SSIS Foreach循环容器与。 对于循环容器 (SSIS Foreach Loop Container Vs. For Loop Container)

With this description of both containers, you can see that each has its own use case. In this section, I will mention some examples of using each container.

通过这两个容器的描述,您可以看到每个容器都有自己的用例。 在本节中,我将提到一些使用每个容器的示例。

One of the most efficient uses of SSIS For Loop Container is loading data as chunks; this approach is used to minimize the memory load when handling a huge volume of data. You can refer to our previously published article for more information: SQL OFFSET FETCH Feature: Loading Large Volumes of Data Using Limited Resources with SSIS.

SSIS For循环容器最有效的用途之一是将数据作为块加载。 当处理大量数据时,此方法用于最大程度地减少内存负载。 您可以参考我们先前发布的文章以获取更多信息: SQL OFFSET FETCH功能:使用SSIS使用有限的资源加载大量数据 。

SSIS Foreach Loop Container is more widely used than the Fr Loop Container since it can perform more popular actions, such as looping over files within a directory or looping over an Execute SQL Task result set. The following articles contain some examples of using SSIS Foreach Loop Container:

SSIS Foreach循环容器比Fr循环容器更广泛地使用,因为它可以执行更流行的操作,例如循环遍历目录中的文件或循环遍历Execute SQL Task结果集。 以下文章包含使用SSIS Foreach循环容器的一些示例:

  • Loop through Excel Files and Tables by Using a Foreach Loop Container 使用Foreach循环容器循环浏览Excel文件和表格
  • How to import text files with the same name and schema but different directories into database? 如何将名称和架构相同但目录不同的文本文件导入数据库?
  • Looping Through a Result Set with the Foreach Loop 使用Foreach循环遍历结果集

目录 (Table of contents)

SSIS OLE DB Source: SQL Command vs Table or View
SSIS Expression Tasks vs Evaluating variables as expressions
SSIS OLE DB Destination vs SQL Server Destination
Execute SQL Task in SSIS: SqlStatementSource expression vs Variable Source Type
Execute SQL Task in SSIS: Output Parameters vs Result Sets
SSIS Derived Columns with Multiple Expressions vs Multiple Transformations
SSIS Data types: Change from the Advanced Editor vs Data Conversion Transformations
SSIS Connection Managers: OLE DB vs ODBC vs ADO.NET
SSIS Flat Files vs. Raw Files
SSIS Foreach Loop vs For Loop Container
SSIS: Execute T-SQL Statement Task vs Execute SQL Task
SSIS OLE DB来源:SQL命令与表或视图
SSIS表达式任务与将变量作为表达式求值
SSIS OLE DB目标与SQL Server目标
在SSIS中执行SQL任务:SqlStatementSource表达式与可变源类型
在SSIS中执行SQL任务:输出参数与结果集
具有多个表达式与多个转换的SSIS派生列
SSIS数据类型:高级编辑器的更改与数据转换的转换
SSIS连接管理器:OLE DB与ODBC与ADO.NET
SSIS平面文件与原始文件
SSIS Foreach循环与For循环容器
SSIS:执行T-SQL语句任务与执行SQL任务

翻译自: https://www.sqlshack.com/ssis-foreach-loop-vs-for-loop-container/

ssis for循环容器

你可能感兴趣的:(ssis for循环容器_SSIS Foreach循环与For循环容器)