Run an SQL Query With a Parameter from Excel 2007

157,466

Solution 1

Dunno why MS has made this so complicated, You will have to use Microsoft Query.

Click on Data -> From External Sources -> From Microsoft Query. CHoose Data source comes up. Select SQL Server, enter the Auth details, and select the table

enter image description here

Click on Next, don't select any filtering criteria, choose sort by criteria, click on next. Now, click on View/Edit in MS Query instead of selecting Return to Excel

enter image description here

Click on Finish. Now in MS Query, Click on Criteria -> Add Criteria, choose the operator and let the value be []

enter image description here

Click on File -> return data to Excel. Now Excel should prompt you for the parameter, select the relevant cell

enter image description here

To edit the parameters, click on Data -> Properties -> Finger icon -> Definition -> parameters

enter image description here


You can also use the SQL query editor and type in the query with the joins and put a ? against the field where the parameter has to be fetched.

enter image description here

Solution 2

It's never too late for a shameless plug...

There's this great little plugin for Excel...(disclaimer: I'm the author)

It's an Excel plugin that adds a database engine and an SQL IDE to Excel.

It lets you do SQL operations on anything that is marked as a table inside Excel. You can query across workbooks, and also use tables from an external database (SQL Server/Sqlite/Postgres/Access/MySql) in the same query.

It has SQL syntax highlighting, statement completion and a bunch of other cool stuff. It uses SQLite under the hood, but can use external db engines to process the data as well.

I do charge for it tho, but there's a free trial which you can use for 30 days. It's called ThingieQuery.

Sorry about the plug, hopefully it's not too inappropriate.

Share:
157,466

Related videos on Youtube

Vaccano
Author by

Vaccano

I am an IT Software Architect from Salt Lake City, Utah.

Updated on September 17, 2022

Comments

  • Vaccano
    Vaccano almost 2 years

    I have a query in an Excel table. I followed this to do it.

    But how can I add a parameter from a cell into my query?

    I am looking for a way to do this:

    select *
    from dbo.Customers
    where CustomerID = Cell.A2
    

    The Cell.A2 does not work. But how can I add in something like it, so that the contents of a cell are used in my query?

    • Admin
      Admin about 9 years
      Have you tried just naming the cell - then using the name in the query instead of the cell reference?
  • Vaccano
    Vaccano over 13 years
    Alas, parameters is disabled.
  • 100rabh
    100rabh over 13 years
    @Vaccano: Revamped my answer, please have a look
  • Vaccano
    Vaccano over 13 years
    Thanks for the effort on this Sathya. Alas I realized that because my query has some joins in it MS Query cannot display it graphically. That makes it so you cannot have parameters. I am going to make it as a VBA function. I am giving you the nod though because your answer was so complete.
  • 100rabh
    100rabh over 13 years
    @Vaccano: You can write the query using the SQL editor, and put a ? against the field to which the parameter has to be selected i.imgur.com/NgXxc.png
  • mikker
    mikker over 13 years
    I have found that once the query is in Excel, you can go into the query parameters screen and edit it there without going into MS Query again. Just drop a ? where the criteria goes. Then go back into the connection properties screen and assign that parameter to a cell in the spreadsheet.
  • 100rabh
    100rabh over 13 years
    @Tracy yeah, that works as well!
  • slotishtype
    slotishtype almost 13 years
    Where all the images go?
  • 100rabh
    100rabh almost 13 years
    @slotishtype fixed. Side effect of this