简体   繁体   English

将多个查询结果导出到单个Excel文件

[英]Export Multiple Query Results to Single Excel File

I ran a sp in SSMS and it gathers information from 50+ databases with the exact same structure. 我在SSMS中运行了一个sp,它从50多个具有完全相同结构的数据库中收集信息。 I am pulling results such as CustomerName, NumberOfUsers and VersionofCode. 我正在提取诸如CustomerName,NumberOfUsers和VersionofCode之类的结果。 When I execute the procedure, I get 50+ different result sets, all with the same columns selected. 执行该过程时,我得到50多个不同的结果集,所有结果集都选择了相同的列。 Instead of exporting these 50+ times and putting it together in a single excel sheet, I'd like to see if I can export all results to 1 excel file. 我不想将这些结果导出50多次并将其放到一个excel表中,而是想将所有结果导出到1个excel文件中。

Is this possible? 这可能吗? I would have to think there would be a way to do this as my column names match up for every database I am querying. 我将不得不考虑这样做,因为我的列名与我查询的每个数据库都匹配。

Any help is appreciated! 任何帮助表示赞赏!

There are probably a number of ways to solve this problem. 解决此问题的方法可能有很多。 I would address the issue by attempting to merge the many result sets from your stored procedure calls into a single result set and then perform whatever output-export (to excel) that you wish to do. 我将通过尝试将存储过程调用中的许多结果集合并到单个结果集中,然后执行您想要执行的任何输出导出(以实现卓越效果)来解决该问题。

Simplest method would be to use a temp table to accumulate the results from each stored proc call. 最简单的方法是使用临时表来累积每个存储的proc调用的结果。 You can use the "INSERT #temptable EXEC mystoredproc @param1" syntax to store the results of a stored proc. 您可以使用“ INSERT #temptable EXEC mystoredproc @ param1”语法来存储存储过程的结果。

Here's a little example I whipped up: 这是我举个例子:

-- *** 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

The resulting output from that final SELECT will be a combined, single result set from both stored procedure calls. 该最终SELECT的结果输出将是来自两个存储过程调用的组合的单个结果集。

I hope this helps. 我希望这有帮助。

I combined the suggestion to use a temp table with an example from this blog post that uses sys.sp_msforeachdb to iterate through databases, resulting in the following: 我将使用临时表的建议与此博客文章中的示例结合使用,该示例使用sys.sp_msforeachdb遍历数据库,结果如下:

Create TABLE #temp
(
    DbName NVARCHAR(50),
    TableName NVARCHAR(50)
)

INSERT #temp

EXEC
    sys.sp_msforeachdb 
    'SELECT ''?'' DatabaseName, Name FROM [?].sys.Tables WHERE Name LIKE ''%sometablename%'''

Select * from #temp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM