Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
499 views
in Technique[技术] by (71.8m points)

html - 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. The SP accepts an int parameter but I cannot find a way to call it in the query.

Latest effort-

EXEC sp_makewebtask 
    @outputfile = 'C:UsersmeDocumentsesting.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.

Thanks for your time,

H

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

image

And, if you use parameters from sheet then, for my example,

image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...