1

I have an Excel file with an SQLOLEDB connection to a MS SQL server,

When I do this with an Oracle link I can simply put WHERE XXX = ? in my query to prompt for values in Excel, however doing it with MSSQL I am getting the following error:

No Value given for one or more required parameters

When trying to parameterise a basic excel query from a value to ?

If i use values like this I get results:

SELECT * 
FROM srv.stats
WHERE srv.stats.[year] = '2016'
AND srv.stats.[quarter] = '1'

When I add in the parameter ? I get the above error.

SELECT * 
FROM srv.stats
WHERE srv.stats.[year] = ?
AND srv.stats.[quarter] = ?

I want to be able to do this without using VB.

Matt
  • 14,906
  • 27
  • 99
  • 149

2 Answers2

5

Since MS SQL sources from ODBC connections don't inherently allow you to use the WHERE xxx = ? code in your SQL query (unlike Oracle connections) you need to spoof Excel to allow parameters from a MS SQL data source by building a Microsoft Query and then overwriting it.

Open a new Excel file and go to the Data tab, choose the From Other Sources drop down and select From Microsoft Query.

enter image description here

The Choose Data Source window will appear. Choose an existing datasource or set up a new connection to your server by selecting <New Data Source> and click OK.

enter image description here

Once done you will see the query wizard window open to select tables and columns, as you're going to be adding your own SQL query later just select one table that is in your query and add it to the Columns in your query: section by using the > button. For the next 2 windows just click Next and then finally Finish.

enter image description here

You will then be prompted to select how you want to view the data in the Import Data window, first of all click the Properties... button.

enter image description here

And then the Definition tab, in the Command text: box you will have a SELECT statement, below there you will need to add WHERE clauses for the amount you have in your actual query. These need to be added in the format of:

WHERE 1 = ?
AND 2 = ?
AND 3 = ?

enter image description here

Once this is done click OK to go back to the Import Data window and select your output type; Table, PivotTable report or PivotChart and PivotTable Report depending on what how you want to display your data.

You will then be prompted to enter a value for each parameter. If you are getting these values from Cells choose the location of them now in the order you will be putting in your actual parameters. When you have entered your parameter sources go back to the Data tab and click connections and then into the definition tab.

Now in the Command text: box paste in your actual SQL query with your parameters in the WHERE clause = ? and in the same order your defined the sources and click OK, Your data should now populate as it usually does with your parameters being used.

Matt
  • 14,906
  • 27
  • 99
  • 149
  • _Since MS SQL sources from ODBC connections don't inherently allow you to use the WHERE xxx = ? code in your SQL query_ <- looks like a problem in specific environment, I never had a problem with `WHERE xxx = ?` clause and SQL Server driver, can't reproduce the issue you describe. – BrakNicku Feb 11 '16 at 18:58
  • Is that from a separate server odbc connection? – Matt Feb 12 '16 at 22:27
  • Yes, separate server, default "SQL Server" ODBC driver. It worked well on different OS, different Excel versions and SQL Server 2005-2012 versions. – BrakNicku Feb 13 '16 at 13:58
  • Strange. Couldn't get this to work myself. And seemed to be the case with other issues I found when searching – Matt Feb 14 '16 at 11:28
2

There is no way to prompt for inputs directly in SQL Server in the same way that you can in Access for example. You can make the values that are passed into queries like this.

DECLARE @year SMALLINT
SET @year = 2016

DECLARE @quarter TINYINT
SET @quarter = 1

SELECT * 
FROM srv.stats
WHERE srv.stats.[year] = @year
  AND srv.stats.[quarter] = @quarter;

You then just need to find a way that suits your solution (Excel?) to pass these. This might, for example, take the form of a stored procedure.

CREATE PROCEDURE testProcedure @year SMALLINT, @quarter TINYINT
AS
    SELECT * 
    FROM srv.stats
    WHERE srv.stats.[year] = @year
      AND srv.stats.[quarter] = @quarter
GO;
Simon Ridd
  • 116
  • 5
  • I have no issue using the above query style with `?` ~ `WHERE srv.stats.[year] = ?` when using Excel with Oracle? Is it MSSQL that causes this error? – Matt Feb 08 '16 at 11:19
  • Yes, as per my answer you cannot request the values for your prompts like this in SQL Server. You can pass the actual values you want to a stored procedure, also as per my answer. Can you not pass the values you want via the VBA/C# script either directly in the SQL or via parameters? If not please provide the reason why and details of the method you're using :) – Simon Ridd Feb 08 '16 at 12:06
  • no vb at the minute as i wasnt aware MS SQL wasnt able to run parameters from `?` – Matt Feb 08 '16 at 13:02
  • OK, please bear in mind I'm not an expert in VBA and this code isn't tested. The bulk of it comes from the accepted answer to this question http://stackoverflow.com/questions/1120674/accessing-sql-database-in-excel-vba – Simon Ridd Feb 08 '16 at 13:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102905/discussion-between-simon-ridd-and-matt). – Simon Ridd Feb 08 '16 at 13:21