Running a SQL Stored Procedure from Excel with Dynamic Parameters

December 12th, 2011 by Leave a reply »

In my life-changing post, Running a SQL-Stored Procedure from Excel, I showed you how to make Excel interact with a SQL server Stored Procedure.

.

Since that post, I've had several requests to cover the topic of passing dynamic parameters to a Stored Procedure. So today, I will FINALLY show you.

.

Step 1: Data tab – > From Other Sources -> From SQL Server

.

Step 2: Enter Credentials.

.

Step 3: Choose any small table or view. It doesn't matter which one because we'll be changing the connection anyway.

.

Step 4: In the Data Connection Wizard dialog, give your connection a friendly name that you can remember. This is how you'll point to this connection in VBA. In this case, I'm calling this connection MYSERVER.


.

Step 5: Excel will pop up the Import Data dialog box - Click Properties here (NOT THE OK BUTTON).

.

Step 6: Click on the Definition tab. Here, you'll want to change Command Type to SQL, and then enter your Stored Procedure name in the Command Text input. As you can see, the SQL statement is simply a call to the Stored Procedure along with the Parameter name (in this case, the procedure is expecting one parameter that accepts a market name).

.

Step 7: Excel warns you about something unimportant - Click Yes (which in this case, means go away).

.

Step 8: Excel will activate the Import Data dialog box again. This time click OK to fire the Stored Procedure and return the results.

.

Step 9: Note the results you get from your original hard-coded command you entered in Step 6. At this point, you know your connection is working.

.

Step 10: Create a mechanism to dynamically select and enter your parameters.

In my case, I need to pass a market name to my Stored Procedure. So I added a simple dropdown where my selection goes to Cell B2. The mechanism you choose to select your parameters is not the important thing here. The important thing is to note where the final selection will be housed. In this example, my market selection will end up in Cell B2.

.

Step 11: The final step is to copy and paste this Macro into a standard module. Notice that all we are dong here is changing the definition fo the CommandText property of the Connection. This basically means that as long as you can point to the correct connection (MYSERVER in this case), you can rebuild the CommandText on the fly. Here, I'm rebuilding the CommandText to point to my chosen cell range (cell B2 - where my market selection is housed).

  1. Sub RefreshQuery()
  2.  
  3.     With ActiveWorkbook.Connections("MYSERVER").OLEDBConnection
  4.         .CommandText = "EXECUTE dbo.Tng_Market_Feed '" & Range("B2").Value & "'"
  5.     End With
  6.     ActiveWorkbook.Connections("MYSERVER").Refresh
  7.    
  8. End Sub

.

At this point, it's just a question of firing the Macro each time your parameter selection changes.

Now you're cooking with gas!

.

Next Step is to check out:
Running Advanced Stored Procedures from Excel

Advertisement

55 comments

  1. Chris says:

    Hi,

    Further to my query on 14 May 2014, I have since re-written my stored procedure to output the results to separately stored tables within the same database. There are around 15 of these output tables. I then have a data connection in Excel for each of these output tables.

    I have something odd happening that I would appreciate any help you can provide. Apologies if the below is a bit long-winded, but I’ve tried to make it as clear as possible:

    My stored procedure takes four variables – @Service (string), @ReportStartDate (datetime), @ReportEndDate (datetime) and @Internal (string). When I refresh the stored procedure connection via VBA (with the variable values derived from specific cells on a worksheet), it runs fine for all values assigned to the @Service variable apart from one (all the other variables remain the same regardless of which value is assigned to @Service), where it seems to update all output tables apart from one. Yet when I run the stored procedure manually from within SQL Server using the same variable values, all output tables are populated correctly. I just cannot see why this output table is not being populated for one set of variables when it is for all other sets of variables?

    Any ideas as to why this is happening? I can’t post any code as it contains confidential company information.

  2. Perry says:

    First off… thank you! This is helping me quite a bit.

    One problem (you know that was coming)… It works wonderful in Excel 2013. In Excel 2010 and 2007, we get an Excel Run Time Error 80010108 on the refresh. We must then close ALL open Excel worksheets to do ANY refreshes.

    Very odd… but I guess this is why they call it the bleeding edge. :)

  3. Perry says:

    Well, I found the solution. There is a compatibility issue with 2013 and 2010.

    In Excel 2013, you now have a new checkbox option in the data connection. The new option is whether to refresh a specific connection if a “refresh all” is used. I did NOT want to do that, so I clicked it off in 2013. Bad choice. This is what stopped the refresh in 2010 from working. Once I turned that on, the refresh worked.

    Ugh!

Leave a Reply