仅当给定查询不存在记录时,如何继续执行下一个任务?
我有以下 SQL 语句来检查是否存在重复记录。如何查看是否没有返回记录?我在 SSIS 包中使用它。我只希望它在不存在记录的情况下继续下一步,否则错误。
SELECT Number
, COUNT(Number) AS DuplicateCheckresult
FROM [TelephoneNumberManagement].[dbo].[Number]
GROUP BY Number
HAVING COUNT(Number) > 1
I have the following piece of SQL that will check if any duplicate records exist. How can I check to see if no records are returned? I'm using this in an SSIS package. I only want it to proceed to the next step if no records exist, otherwise error.
SELECT Number
, COUNT(Number) AS DuplicateCheckresult
FROM [TelephoneNumberManagement].[dbo].[Number]
GROUP BY Number
HAVING COUNT(Number) > 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
以下使用
SSIS 2008 R2
和SQL Server 2008 R2
后端创建的示例说明了如何在 SSIS 包中实现您的要求。创建一个名为
dbo.Phone
的表,并在其中填充一些将返回重复结果的记录。您需要稍微修改您的查询,以便它返回重复的总数而不是重复的行。此查询将仅产生一个值(标量值),该值可能为零或非零值,具体取决于是否找到重复项。这是我们将在 SSIS 包的执行 SQL 任务中使用的查询。
在 SSIS 包上,创建一个名为
DuplicatesCount
且数据类型为 Int32 的变量。在 SSIS 包上,创建 OLE DB 连接管理器以连接到 SQL Server 数据库。我将其命名为SQLServer。
在 SSIS 的“控制流”选项卡上,打包、放置一个执行 SQL 任务并按如下所示进行配置:截图。该任务应接受单个行值并将其分配给新创建的变量。将
ResultSet
设置为单行。将连接设置为 SQLServer,并将 SQLStatement 设置为 SELECT COUNT(Number) AS Duplicates FROM (SELECT Number, COUNT(Number) AS NumberCount FROM dbo.Phone GROUP BY Number HAVING COUNT(Number) > ; 1)T1。在结果集部分,单击添加按钮并将结果名称设置为
0
。将变量User::DuplicatesCount
分配给结果名称。然后单击“确定”。在执行 SQL 任务之后放置另一个任务。我选择了 Foreach Loop Container 作为示例。如下所示连接任务。
现在,要求是是否没有重复项,这意味着执行中查询的输出值是否SQL 任务为零,则包应继续到 Foreach 循环容器。否则,包不应进入 Foreach 循环容器。为此,我们需要向优先约束添加一个表达式(任务之间的绿色箭头)。
右键单击优先约束并选择
Edit...
在优先约束编辑器上,从求值操作下拉列表中选择
表达式
。将表达式设置为@[User::DuplicatesCount] == 0
以检查变量DuplicatesCount
是否包含值零。值零表示表dbo.Phone
中没有重复项。测试表达式以验证语法是否正确。单击“确定”关闭验证消息。单击“确定”关闭优先级约束。现在,控制流应如下所示。优先约束将用fx表示,它表示存在一个约束/表达式。
让我们检查一下表
dbo.Phone
中的行。如您所见,值1234567890
存在两次。这意味着存在重复行,并且 Foreach 循环容器不应执行。让我们执行该包。您可以注意到执行 SQL 任务已成功执行,但它没有继续到 Foreach 循环容器。这是因为变量 DuplicatesCount 包含值 1,并且我们编写了一个条件来检查该值是否应为零才能继续到 Foreach 循环容器。
让我们从表 dbo.Phone 中删除行,并使用以下脚本用非重复行填充它。
现在,表中的数据如下所示。
如果我们执行该包,它将继续到 Foreach 循环容器,因为在table
dbo.Phone
希望有所帮助。
Following example created using
SSIS 2008 R2
andSQL Server 2008 R2
backend illustrates how you can achieve your requirement in an SSIS package.Create a table named
dbo.Phone
and populate it couple records that would return duplicate results.You need to slightly modify your query so that it returns the total number of duplicates instead of the duplicate rows. This query will result only one value (scalar value) which could be either zero or non-zero value depending on if duplicates are found or not. This is the query we will use in the SSIS package's Execute SQL Task.
On the SSIS package, create a variable named
DuplicatesCount
of data type Int32.On the SSIS package, create an OLE DB Connection manager to connect to the SQL Server database. I have named it as SQLServer.
On the Control Flow tab of the SSIS, package, place an Execute SQL Task and configure it as shown below in the screenshots. The task should accept a single row value and assign it to the newly create variable. Set the
ResultSet
to Single row. Set the Connection toSQLServer
and the SQLStatement toSELECT COUNT(Number) AS Duplicates FROM (SELECT Number, COUNT(Number) AS NumberCount FROM dbo.Phone GROUP BY Number HAVING COUNT(Number) > 1) T1
.On the Result Set section, click on the Add button and set the Result Name to
0
. Assign the variableUser::DuplicatesCount
to the result name. Then click OK.Place another task after the Execute SQL Task. I have chosen Foreach Loop Container for sample. Connect the tasks as shown below.
Now, the requirement is if there are no duplicates, which means if the output value of the query in the Execute SQL task is zero, then the package should proceed to Foreach loop container. Otherwise, the package should not proceed to Foreach loop container. To achieve this, we need to add a expression to the precedence constraint (the green arrow between the tasks).
Right-click on the precedence constraint and select
Edit...
On the Precedence constraint editor, select
Expression
from the Evaluation operation dropdown. Set the expression to@[User::DuplicatesCount] == 0
in order to check that the variableDuplicatesCount
contains the value zero. Value zero means that there were no duplicates in the tabledbo.Phone
. Test the expression to verify that the syntax is correct. Click OK to close the verification message. Click OK to close the precedence constraint.Now, the Control Flow should look like this. The precedence constraint will be denote with fx, which represents there is a constraint/expression in place.
Let's check the rows in the table
dbo.Phone
. As you see, the value1234567890
exists twice. It means that there are duplicate rows and the Foreach loop container shouldn't execute.Let's execute the package. You can notice that the Execute SQL Task executed successfully but it didn't proceed to Foreach Loop container. That's because the variable DuplicatesCount contains a value of 1 and we had written a condition to check that the value should be zero to proceed to Foreach loop container.
Let's delete the rows from the table dbo.Phone and populate it with non-duplicate rows using the following script.
Now, the data in the table is as shown below.
If we execute the package, it will proceed to the Foreach Loop container because there are no duplicate rows in the table
dbo.Phone
Hope that helps.
您需要做的是使用
@@ROWCOUNT
,但具体操作方式取决于您的数据流。看一下这个讨论,它指出了如何使用一个或两个数据流来实现这一点。使用行计数SSIS
What you need to do to is work with
@@ROWCOUNT
, but how you do it depends on your data flows. Have a look at this discussion, which points out how to do it with either one or with two data flows.Using Row Count In SSIS