简体   繁体   English

在SSMS中将存储过程结果集导出到Excel

[英]Export stored procedure result set to Excel in SSMS

i'm using SSMS and attempting to export the results of a stored procedure to a new excel file.我正在使用 SSMS 并尝试将存储过程的结果导出到新的 excel 文件。 The SP accepts an int parameter but I cannot find a way to call it in the query. SP 接受一个 int 参数,但我找不到在查询中调用它的方法。

Latest effort-最新努力-

EXEC sp_makewebtask 
    @outputfile = 'C:\Users\me\Documents\testing.xls', 
    @query = **ExportAsExcel** N'@id' = 123
    @colheaders =1, 
    @FixedFont=0,@lastupdated=0,@resultstitle='Testing details'

Running the stored procedure results in two tables of data, which I need on separate sheets.运行存储过程会产生两个数据表,我需要在单独的工作表上。 Can any of you advise a better way to go about this?你们中的任何人都可以提出更好的方法来解决这个问题吗? It doesn't even need to be automated, I just need to get the correct data.它甚至不需要自动化,我只需要获取正确的数据。 The sp name is bolded above. sp 名称在上方以粗体显示。

Thanks for your time,谢谢你的时间,

H H

I suggest you split your stored procedure into two procedures that each respectively return a separate table and have those called to different worksheets.我建议您将存储过程拆分为两个过程,每个过程分别返回一个单独的表并将它们调用到不同的工作表。

There are a variety of ways to return data to Excel using SQL有多种方法可以使用 SQL 将数据返回到 Excel

Here is a favourite of mine from code by Joshua (you don't have to use the parameters):这是Joshua 的代码中我最喜欢的(您不必使用参数):

  1. Select the Data tab on Excel's Ribbon, then within the Get Exernal Data group choose the "From other Sources" drop-down.选择 Excel 功能区上的“数据”选项卡,然后在“获取外部数据”组中选择“来自其他来源”下拉列表。 Then Choose "From Microsoft Query"然后选择“来自 Microsoft Query”

  2. Within "Choose Data Source" pop-up box, select your SQL Server, then hit OK.在“选择数据源”弹出框中,选择您的 SQL Server,然后点击“确定”。

  3. Close the "Add Tables" popup if necessary.如有必要,关闭“添加表”弹出窗口。

  4. Click on the "SQL" button, or choose View > SQL to open the SQL pop-up editor.单击“SQL”按钮,或选择“查看”>“SQL”以打开 SQL 弹出式编辑器。

  5. Enter the following syntax: {CALL myDatabaseName.dbo.myStoredProc (?, ?, ?)}输入以下语法:{CALL myDatabaseName.dbo.myStoredProc (?, ?, ?)}

    For example: {CALL northwind.dbo.spGetMaxCost (?, ?, ?)}例如:{CALL northwind.dbo.spGetMaxCost (?, ?, ?)}

    Be sure to include the squiggly braces around the call statement.确保在 call 语句周围包含波浪形大括号。 Each Question Mark (?) indicates a parameter.每个问号 (?) 表示一个参数。 If your stored procedure calls for more or less parameters, add or subtract question marks as needed.如果您的存储过程需要更多或更少的参数,请根据需要添加或减去问号。

  6. Hit the OK button.点击确定按钮。 A question box should pop-up saying "SQL Query can't be represented graphically, continue anyway?", just hit the OK button.应该会弹出一个问题框,上面写着“SQL 查询无法以图形方式表示,还要继续吗?”,只需点击“确定”按钮即可。

  7. You will now be asked for sample parameters for each question mark you included above.现在将要求您提供上面包含的每个问号的示例参数。 Enter valid parameter values for the data you are querying.为您要查询的数据输入有效的参数值。

  8. Once you have entered the last parameter, you should get some results back in Microsoft Query.输入最后一个参数后,您应该会在 Microsoft Query 中返回一些结果。 If they look good, close Microsoft Query.如果它们看起来不错,请关闭 Microsoft Query。

  9. You should now be looking at an "Import Data" pop-up.您现在应该看到“导入数据”弹出窗口。 Click the Properties button, which will bring up the "Connection Properties" pop-up.单击“属性”按钮,这将显示“连接属性”弹出窗口。

  10. Select the Definition tab, then select the Parameters button.选择定义选项卡,然后选择参数按钮。 You should now see a "Parameters" pop-up, where you can connect the parameter to a specific cell.您现在应该会看到一个“参数”弹出窗口,您可以在其中将参数连接到特定单元格。

  11. Select Get the value from the following cell, and then connect to an appropriate cell in Excel that will hold your parameter, by clicking the little box with the arrow.选择从以下单元格获取值,然后通过单击带有箭头的小框连接到 Excel 中将保存参数的适当单元格。

  12. If you want the data to refresh every time you change the cell containing the parameter, check the box stating "Refresh automatically when cell value changes"如果您希望每次更改包含参数的单元格时都刷新数据,请选中“单元格值更改时自动刷新”框

  13. Continue as above for the other parameters.继续如上处理其他参数。 When finished, click OK, to return to the Connection Properties pop-up.完成后,单击“确定”以返回“连接属性”弹出窗口。 Click OK to return to the Import Data pop-up, and click OK again.单击“确定”返回“导入数据”弹出窗口,然后再次单击“确定”。

  14. You should now have some data straight from your stored procedure.您现在应该直接从您的存储过程中获得一些数据。

You will end up with connection information similar to:您最终将获得类似于以下内容的连接信息:

Connection info连接信息

And, if you use parameters from sheet then, for my example,而且,如果您使用工作表中的参数,那么,例如,

This isn't a direct answer to your question, but it is possible using Excel VBA and connections to connect to a SQL Server Stored Procedure, feed it parameters, and return the SP result set in Excel.这不是您问题的直接答案,但可以使用 Excel VBA 和连接来连接到 SQL Server 存储过程,为其提供参数,并在 Excel 中返​​回 SP 结果集。 Check out my article Microsoft Excel & SQL Server: Self service BI to give users the data they want for an image and code-heavy demo.查看我的文章Microsoft Excel 和 SQL Server:自助服务 BI,为用户提供所需的数据,用于图像和代码繁重的演示。

Good luck.祝你好运。

There's too much detail there to post in a single SO question, otherwise I'd do that here.那里有太多细节无法在一个 SO 问题中发布,否则我会在这里做。

I develop SSMSBoost add-in and we have implemented the functionality, that allows you to export data to excel in 3 ways (including creation of several worksheets in one file):我开发了 SSMSBoost 插件,我们已经实现了该功能,允许您以 3 种方式将数据导出到 excel(包括在一个文件中创建多个工作表):

  1. You can export all result grids in one operation to "open worksheet" file format, which excel understands and displays correctly.您可以通过一次操作将所有结果网格导出为“打开工作表”文件格式,该文件格式 excel 理解并正确显示。 This file format supports multiple worksheets.此文件格式支持多个工作表。 to use it: right-click the data grid in SSMS and select "Script grid data"-> "Excel" template->All Grids->ToDisk.使用方法:在SSMS中右击数据网格,选择“脚本网格数据”->“Excel”模板->所有网格->ToDisk。 You can also look inside the generated files to understand how it works.您还可以查看生成的文件以了解其工作原理。 You can then implement same functionality in your stored procedure if you want to stay independent of add-ins.如果您想保持独立于加载项,则可以在存储过程中实现相同的功能。 Sample XML is also provided below.下面还提供了示例 XML。 (2 excel sheets with 1 column name and 1 value) (2 个 excel 表,1 个列名和 1 个值)

 <?xml version="1.0"?> <?mso-application progid="Excel.Sheet"?> <Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40"> <ExcelWorkbook xmlns="urn:schemas-microsoft-com:office:excel"> </ExcelWorkbook> <Styles> <Style ss:ID="sH1"><Font ss:Bold="1"/></Style> <Style ss:ID="sD1"><NumberFormat ss:Format="General Date"/></Style> <Style ss:ID="sD2"><NumberFormat ss:Format="Short Date"/></Style> <Style ss:ID="sD3"><NumberFormat ss:Format="Short Time"/></Style> </Styles> <Worksheet ss:Name="GridResults1"> <Table> <Row> <Cell ss:StyleID="sH1"><Data ss:Type="String">ColumnNameA</Data></Cell> </Row> <Row> <Cell><Data ss:Type="Number">1</Data></Cell> </Row> </Table> </Worksheet> <Worksheet ss:Name="GridResults2"> <Table> <Row> <Cell ss:StyleID="sH1"><Data ss:Type="String">ColumnNameB</Data></Cell> </Row> <Row> <Cell><Data ss:Type="Number">1</Data></Cell> </Row> </Table> </Worksheet> </Workbook>

  1. You can also copy-paste data from SSMS Grid, right-clicking it and choosing "Copy selection as XML Spreadsheet (Excel)".您还可以从 SSMS 网格复制粘贴数据,右键单击它并选择“将选择复制为 XML 电子表格 (Excel)”。 Data will be copied preserving data types.数据将被复制保留数据类型。

  2. More advanced option is our "Post execution handlers" functionality.更高级的选项是我们的“执行后处理程序”功能。 It allows you to run certain actions after query execution completes.它允许您在查询执行完成后运行某些操作。 You can schedule automatic export to excel file here as well.您也可以在此处安排自动导出到 Excel 文件。

Hope this helps, with, or without SSMSBoost ;)希望这会有所帮助,有或没有 SSMSBoost ;)

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

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