UNION 多个存储过程的结果 [英] UNION the results of multiple stored procedures
问题描述
我有一个存储过程,我需要调用几次不同的时间,每次传入不同的参数.我想将结果作为单个数据集收集.这样的事情可能吗...
I have a stored procedure I need to call several different times passing in different paramaters each time. I would like to collect the results as a single dataset. Is something like this possible ...
exec MyStoredProcedure 1
UNION
exec MyStoredProcedure 2
UNION
exec MyStoredProcedure 3
我尝试使用上面的语法但得到了错误...
I tried using the syntax above but got the error ...
Incorrect syntax near the keyword 'UNION'
我正在处理的存储过程非常复杂,对我来说就像一个黑匣子",所以我无法进入存储过程的定义并更改任何内容.关于如何将结果汇总在一起的任何建议?
The stored procedures I am dealing with are pretty complex and sort of a "black box" to me, so I cannot get into the definition of the stored procedure and change anything. Any suggestions on how to gather the results together?
我使用的是 SQL Server 2008 R2.感谢您的帮助.
I am using SQL Server 2008 R2. Thanks for any help.
推荐答案
您必须使用这样的临时表.UNION 用于选择,而不是存储过程
You'd have to use a temp table like this. UNION is for SELECTs, not stored procs
CREATE TABLE #foo (bar int ...)
INSERT #foo
exec MyStoredProcedure 1
INSERT #foo
exec MyStoredProcedure 2
INSERT #foo
exec MyStoredProcedure 3
...
并希望存储的过程没有 INSERT..EXEC..
不能嵌套.或多个结果集.或者其他几个破坏结构
And hope the stored procs don't have INSERT..EXEC..
already which can not be nested. Or multiple resultsets. Or several other breaking constructs
这篇关于UNION 多个存储过程的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!