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提供的类似功能。
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:
有不同类型的循环,例如:
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 is a counter-based loop that is composed of three main phases:
For循环是一个基于计数器的循环,由三个主要阶段组成:
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循环流:
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
}
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
}
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中实现循环:
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 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任务,脚本任务,文件系统任务,执行过程任务等)的形式:
When we open the SSIS For Loop Container editor, there are three main properties that we have to set:
当我们打开SSIS For循环容器编辑器时,必须设置三个主要属性:
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循环,则必须使用以下表达式:
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循环容器更复杂,因为它具有许多用例,并且需要更复杂的配置:
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循环容器编辑器表单的“收集”选项卡中选择枚举器类型:
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循环容器变量映射选项卡页面中的项目,方法是在当前行和变量名称中指定项目索引。
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循环迭代号
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循环容器的一些示例:
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循环容器