14

I need to create a dynamic report using Excel 2010 and SQL Server. Going off of previous ones created by others I figured out how to create the data connection and use the stored procedure with a specific parameter in the connection properties definition tab. The other reports use a ? in the command definition and use a cell in the workbook to define the parameter.

How do I do that too?

Specific steps to creating the connection/report/parameter selection or any help much appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
d456
  • 1,147
  • 4
  • 13
  • 25

1 Answers1

29

I got it to work using the steps provided at the following linked blog post.

http://codebyjoshua.blogspot.com/2012/01/get-data-from-sql-server-stored.html

Steps copied here in case link breaks in the future.

Excel 2007 Instructions:

  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.

d456
  • 1,147
  • 4
  • 13
  • 25
  • 1
    Great stuff! Worked perfectly :) – David C Nov 14 '14 at 15:44
  • The only choices showing in step 2 "Select your SQL server" are "New Data Source", "dBASE Files", "Excel Files" and "MS Access Database". I really wonder how you guys made it work... – RickNo Dec 23 '15 at 13:47
  • I haven't had to use this in 3 years. I only had it work in Excel 2007. Are you using a newer version of Excel? – d456 Dec 29 '15 at 15:06
  • 1
    @RickNo You need to add your Server as an existing dsn connection and then when that box appears, click browse, find your dsn file and that's it! – Gareth Jan 28 '16 at 10:14
  • At step 2, you need to have a data source already configured. (If not, click Data tab/Existing Connections, click 'Browse for more...', click 'New Source...' and create it). After double-clicking it the 'Query Wizard - Choose Columns' dialog appears. Click Cancel, then Yes to the message box that follows. Continue from step 3. Excel 2016. – codah Mar 29 '19 at 04:56