如何从SSIS包中的执行SQL任务向用户变量返回两个记录计数

时间:2014-03-05 06:44:08

标签: sql-server ssis

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

有问题的SQL通过SQL合并语句执行Upsert。我想要做的是返回插入的记录数和更新的记录(没有删除这里要担心)。我正在使用输出选项将更改的recs输出到表变量。

我尝试将值返回为:

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

Select Count(*) as InsertCount from @mergeOutput where Action = 'Insert'

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

我做错了什么?

感谢。

2 个答案:

答案 0 :(得分:1)

您应该尝试以下方法:

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

使用单个结果集,这应该为您提供

行的输出
UpdateCount | InsertCount
#           | #

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

当我想从同一查询中的不同表返回多个结果集时,我就会使用这个,但是我不知道它如何与merge语句的输出一起使用。

答案 1 :(得分:0)

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

SqlCommand as:

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