Sql server 如何将执行SQL任务中的两个记录计数返回给SSIS包中的用户变量

Sql server 如何将执行SQL任务中的两个记录计数返回给SSIS包中的用户变量,sql-server,ssis,Sql Server,Ssis,我在SSIS 2012中有一个包,在控制流级别有一个执行SQL任务 所讨论的SQL通过SQL merge语句执行Upsert。我想做的是返回插入记录和更新记录的计数(这里不需要担心删除)。我使用output选项将更改后的rec输出到一个表变量 我尝试将值返回为: Select Count(*) as UpdateCount from @mergeOutput where Action = 'Update' 及 我尝试将resultset设置为单行集和全行集,但没有看到任何返回到我为它们设置的包

我在SSIS 2012中有一个包,在控制流级别有一个执行SQL任务

所讨论的SQL通过SQL merge语句执行Upsert。我想做的是返回插入记录和更新记录的计数(这里不需要担心删除)。我使用output选项将更改后的rec输出到一个表变量

我尝试将值返回为:

Select Count(*) as UpdateCount from @mergeOutput where Action = 'Update'

我尝试将resultset设置为单行集和全行集,但没有看到任何返回到我为它们设置的包变量(intInsertcount和intUpdatecount)的内容

我做错了什么


谢谢。

您应该尝试以下方法:

Select UpdateCount = (Select Count(*) as UpdateCount from @mergeOutput where Action = 'Update'),
InsertCount = (Select Count(*) as InsertCount from @mergeOutput where Action = 'Insert')
使用一个结果集,这将为您提供一个大致如下的输出:

UpdateCount | InsertCount
#           | #
然后只需映射结果集,更改每个结果的名称,并使用断点测试并确保变量在整个过程中更新


当我想在同一个查询中从不同的表返回多个结果集时,我会使用这个命令,但是我不知道它如何处理合并语句的输出。

执行SQL命令设置为单行输出

SqlCommand作为:

Select sum(case when Action='Update' then 1 else 0 end) Update_count,
sum(case when Action='Insert' then 1 else 0 end) Insert_count 
from @mergeOutput;
结果集选项卡中,单击添加按钮;将变量设置为按位置指向上述查询的2个输出:


0=>intUpdatecount;1=>intInsertcount

我看不出他的解决方案有任何问题。不知道为什么有人否决了它。谢谢彼得,我试试看。但是你能解释一下我是如何将它映射到包变量的吗?
Select sum(case when Action='Update' then 1 else 0 end) Update_count,
sum(case when Action='Insert' then 1 else 0 end) Insert_count 
from @mergeOutput;