可能有很多方法可以解決這個問題。我將通過嘗試將存儲過程調用中的多個結果集合併到單個結果集中,然後執行您希望執行的任何輸出導出(至Excel)來解決該問題。
最簡單的方法是使用臨時表來累積每個存儲的proc調用的結果。您可以使用「INSERT #temptable EXEC mystoredproc @ param1」語法來存儲存儲過程的結果。
這裏有一個小例子,我颳起:
-- *** Create a sample stored proc that returns one result set ***
CREATE PROC spGetCompanyEmployees @pCompanyID AS INT
AS
BEGIN
SELECT Company.CompanyName
, Department.DepartmentName
, Employee.EmployeeName
FROM Company
LEFT JOIN Department ON Department.CompanyID = Company.CompanyID
LEFT JOIN Employee ON Employee.DepartmentID = Department.DepartmentID
WHERE Company.CompanyID = @pCompanyID
END
GO
-- *** Demonstrate how to call that stored proc multiple times,
-- *** accumulating the results in a temp table and selecting
-- *** the combined results at the end.
CREATE TABLE #ttbl
(
CompanyName NVARCHAR(60)
, DepartmentName NVARCHAR(60)
, EmployeeName NVARCHAR(60)
)
INSERT #ttbl
EXEC spGetCompanyEmployees 1
INSERT #ttbl
EXEC spGetCompanyEmployees 2
SELECT * FROM #ttbl
從最終選擇生成的輸出將被組合,單個結果從兩個存儲過程調用集。
我希望這會有所幫助。