仅当给定查询不存在记录时,如何继续执行下一个任务?

发布于 2024-12-02 21:31:17 字数 285 浏览 1 评论 0原文

我有以下 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 技术交流群。

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

发布评论

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

评论(2

情深已缘浅 2024-12-09 21:31:17

以下使用 SSIS 2008 R2SQL Server 2008 R2 后端创建的示例说明了如何在 SSIS 包中实现您的要求。

创建一个名为 dbo.Phone 的表,并在其中填充一些将返回重复结果的记录。

CREATE TABLE [dbo].[Phone](
    [Number] [int] NOT NULL
) ON [PRIMARY]
GO

INSERT INTO dbo.Phone (Number) VALUES 
    (1234567890),
    (1234567890);
GO

您需要稍微修改您的查询,以便它返回重复的总数而不是重复的行。此查询将仅产生一个值(标量值),该值可能为零或非零值,具体取决于是否找到重复项。这是我们将在 SSIS 包的执行 SQL 任务中使用的查询。

SELECT COUNT(Number) AS Duplicates
FROM
(   
    SELECT      Number
            ,   COUNT(Number) AS NumberCount
    FROM        dbo.Phone
    GROUP BY    Number
    HAVING      COUNT(Number) > 1
) T1 

在 SSIS 包上,创建一个名为 DuplicatesCount 且数据类型为 Int32 的变量。

Variable

在 SSIS 包上,创建 OLE DB 连接管理器以连接到 SQL Server 数据库。我将其命名为SQLServer

Connection Manager

在 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 任务

输入 SQL 查询

执行 SQL 任务结果集

在执行 SQL 任务之后放置另一个任务。我选择了 Foreach Loop Container 作为示例。如下所示连接任务。

Control Flow tab

现在,要求是是否没有重复项,这意味着执行中查询的输出值是否SQL 任务为零,则包应继续到 Foreach 循环容器。否则,包不应进入 Foreach 循环容器。为此,我们需要向优先约束添加一个表达式(任务之间的绿色箭头)。

右键单击优先约束并选择 Edit...

Edit

在优先约束编辑器上,从求值操作下拉列表中选择表达式。将表达式设置为 @[User::DuplicatesCount] == 0 以检查变量 DuplicatesCount 是否包含值。值零表示表 dbo.Phone 中没有重复项。测试表达式以验证语法是否正确。单击“确定”关闭验证消息。单击“确定”关闭优先级约束。

优先约束

测试

现在,控制流应如下所示。优先约束将用fx表示,它表示存在一个约束/表达式。

Constraint

让我们检查一下表 dbo.Phone 中的行。如您所见,值 1234567890 存在两次。这意味着存在重复行,并且 Foreach 循环容器不应执行。

Table data

让我们执行该包。您可以注意到执行 SQL 任务已成功执行,但它没有继续到 Foreach 循环容器。这是因为变量 DuplicatesCount 包含值 1,并且我们编写了一个条件来检查该值是否应为零才能继续到 Foreach 循环容器。

Execution 1

让我们从表 dbo.Phone 中删除行,并使用以下脚本用非重复行填充它。

TRUNCATE TABLE dbo.Phone;

INSERT INTO dbo.Phone (Number) VALUES 
    (1234567890),
    (0987654321);

现在,表中的数据如下所示。

Non-duplicate data

如果我们执行该包,它将继续到 Foreach 循环容器,因为在table dbo.Phone

Execution 2

希望有所帮助。

Following example created using SSIS 2008 R2 and SQL 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.

CREATE TABLE [dbo].[Phone](
    [Number] [int] NOT NULL
) ON [PRIMARY]
GO

INSERT INTO dbo.Phone (Number) VALUES 
    (1234567890),
    (1234567890);
GO

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.

SELECT COUNT(Number) AS Duplicates
FROM
(   
    SELECT      Number
            ,   COUNT(Number) AS NumberCount
    FROM        dbo.Phone
    GROUP BY    Number
    HAVING      COUNT(Number) > 1
) T1 

On the SSIS package, create a variable named DuplicatesCount of data type Int32.

Variable

On the SSIS package, create an OLE DB Connection manager to connect to the SQL Server database. I have named it as SQLServer.

Connection Manager

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 to SQLServer and the SQLStatement to SELECT 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 variable User::DuplicatesCount to the result name. Then click OK.

Execute SQL Task General

Enter SQL Query

Execute SQL Task Result Set

Place another task after the Execute SQL Task. I have chosen Foreach Loop Container for sample. Connect the tasks as shown below.

Control Flow tab

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...

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 variable DuplicatesCount contains the value zero. Value zero means that there were no duplicates in the table dbo.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.

Precedence Constraint

Test

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.

Constraint

Let's check the rows in the table dbo.Phone. As you see, the value 1234567890 exists twice. It means that there are duplicate rows and the Foreach loop container shouldn't execute.

Table data

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.

Execution 1

Let's delete the rows from the table dbo.Phone and populate it with non-duplicate rows using the following script.

TRUNCATE TABLE dbo.Phone;

INSERT INTO dbo.Phone (Number) VALUES 
    (1234567890),
    (0987654321);

Now, the data in the table is as shown below.

Non-duplicate data

If we execute the package, it will proceed to the Foreach Loop container because there are no duplicate rows in the table dbo.Phone

Execution 2

Hope that helps.

月光色 2024-12-09 21:31:17

您需要做的是使用 @@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

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