Export stored procedure result set to Excel in SSMS

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

Here is a favourite of mine from code by Joshua (you don't have to use the parameters):

  1. Select the Data tab on Excel's Ribbon, then within the Get Exernal Data group choose the "From other Sources" drop-down. Then Choose "From Microsoft Query"

  2. Within "Choose Data Source" pop-up box, select your SQL Server, then hit OK.

  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.

  5. Enter the following syntax: {CALL myDatabaseName.dbo.myStoredProc (?, ?, ?)}

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

    Be sure to include the squiggly braces around the call statement. 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.

  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. If they look good, close 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.

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

Good luck.

There's too much detail there to post in a single SO question, otherwise I'd do that here.


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):

  1. You can export all result grids in one operation to "open worksheet" file format, which excel understands and displays correctly. 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. 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. (2 excel sheets with 1 column name and 1 value)

<?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)". 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.

Hope this helps, with, or without SSMSBoost ;)