Export Stored Procedure Result Set to Excel in Ssms

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,

Exporting the output of a stored proc. to excel

You can use INSERT INTO OPENROWSET, for example:

INSERT INTO OPENROWSET ('Microsoft.Jet.OLEDB.4.0', 'Excel 8.0;Database=D:\Output.xls;', 'EXEC   [dbo].[spName] ''param''') EXEC [dbo].[spName] 'param'

You should enable Ad Hoc Distributed Queries before:

EXEC sp_configure 'show advanced options', 1
RECONFIGURE
GO
EXEC sp_configure 'ad hoc distributed queries', 1
RECONFIGURE
GO

SQL Server stored procedure to export Select Result to CSV

The task was I had to export from database some data to .CSV at specified time. In the begining we wanted to use windows scheduler for running stp. The STP had to be able to export data. But I couldn't find a way. Instead the thing what we did was creating simple STP which brings only data . And we created batch file which calls STP and export result to .CSV file. The batch file is simple

sqlcmd -S Etibar-PC\SQLEXPRESS -d MEV_WORK -E -Q "dbo.SelectPeople" -o "MyData1.csv" -h-1 -s"," -w 700

dbo.SelectPeople is STP

Etibar-PC\SQLEXPRESS is Schema

MEV_WORK is Database name.



Related Topics



Leave a reply



Submit