合并存储过程结果集

时间:2019-01-22 23:00:25

标签: sql sql-server tsql stored-procedures

我正在执行返回结果的多个存储过程,但我希望它看起来像一个结果集。我尝试了UNION ALL,但仅适用于SELECT语句。

EXEC TaskSummary @TaskName = "Demo", @Days ="5", @GroupName = "Charlie"

EXEC TaskSummary @TaskName = "Framing", @Days="15", @GroupName = "Charlie"

EXEC TaskSummary @TaskName = "Electrical Rough-In", @Days= 7, @GroupName = "Foxtrot"

EXEC TaskSummary @TaskName = "Insulation", @Days = 2, @GroupName = "Charlie"

EXEC TaskSummary @TaskName = "Exterior Doors", @Days= 2, @GroupName = "Charlie"

EXEC TaskSummary @TaskName = "Install Windows", @Days= 2, @GroupName= "Charlie"

EXEC TaskSummary @TaskName = "Bathroom Tiles", @Days= 6, @GroupName = "Charlie"

EXEC TaskSummary @TaskName = "Prime Walls" , @Days= "2", @GroupName = "Bravo"

EXEC TaskSummary @TaskName = "Painting Interior" , @Days= "3" , @GroupName = "Charlie"

EXEC TaskSummary @TaskName = "Mill Work", @Days="3", @GroupName= "Charlie"

EXEC TaskSummary @TaskName = "Backsplash Install" , @Days= "1" , @GroupName= "Charlie"

EXEC TaskSummary @TaskName = "Electrical Dress-Up" , @Days="1" , @GroupName= "Foxtrot"

EXEC TaskSummary @TaskName = "Interior Hardware Install", @Days= "2" , @GroupName= "Charlie"

EXEC TaskSummary @TaskName = "Final Touch Up", @Days= "2", @GroupName = "Bravo"

EXEC TaskSummary @TaskName = "Punch List", @Days= "2", @GroupName= "Charlie"

2 个答案:

答案 0 :(得分:2)

您可以创建一个临时表并将所有存储过程的结果插入其中,然后执行选择查询:

--CREATE TEMP TABLE
CREATE TABLE #TBL(Column1 ....)

--Eexute all stored procedures with an insert statment
INSERT INTO #TBL
EXEC TaskSummary @TaskName = "Demo", @Days ="5", @GroupName = "Charlie"

INSERT INTO #TBL
EXEC TaskSummary @TaskName = "Framing", @Days="15", @GroupName = "Charlie"

...

--Select data from temp table
SELECT * FROM #TBL

答案 1 :(得分:1)

您可以使用表变量来收集所有结果。

DECLARE @u TABLE
           (<columns as the result of TaskSummary regarding number, order and type>);
INSERT INTO @u
            EXECUTE TaskSummary <parameters>;
...
INSERT INTO @u
            EXECUTE TaskSummary <other parameters>;
SELECT *
       FROM @u;