Passing Parameter to SQL Query using a Question Mark

ExcelGuyDude

Board Regular
Joined
Feb 4, 2015
Messages
205
I'm using Excel 2010. I'm getting an error every time I try and pass a parameter straight through to my SQL query so I don't have to use VBA (very long SQL Code). I'm using an ODBC connection that I connected with the "From Microsoft Query" tool that's available. What I'm trying to do is use the question mark feature to replace the conditions I have currently set (W1 and 100000).

Code:
SET NOCOUNT ON

SELECT ib.Item#
	, ib.AvailQty
INTO #InventoryTable
FROM ITEM_INVENTORY_BALANCES ib WITH(NOLOCK)
WHERE ib.Whse# = 'W1'
	AND ib.AvailQty > 100000

SELECT *
FROM #InventoryTable

DROP TABLE #InventoryTable

When running this code, everything works fine.

What I'd like the code to look like is this:

Code:
SET NOCOUNT ON

SELECT ib.Item#
	, ib.AvailQty
INTO #InventoryTable
FROM DATAWSQL.dbo.ITEM_INVENTORY_BALANCES ib WITH(NOLOCK)
WHERE ib.Whse# = ?
	AND ib.AvailQty > ?

SELECT *
FROM #InventoryTable

DROP TABLE #InventoryTable

When I run try and run this query with the new changes, it gives me these 2 errors:

[Microsoft][ODBC SQL Server Driver]Invalid parameter number
[Microsoft][ODBC SQL Server Driver]Invalid Descriptor Index

I'm not sure if there's a setting I need to activate in Excel, or if this is not even possible to do with Temp tables within the SQL code.

Also, I know it's possible to put the SQL code inside of MS Access then do a pass-through query to Excel. I'm avoiding MS Access on purpose.

Thanks in advance for your help,
GuyDude
 

Excel Facts

Excel Wisdom
Using a mouse in Excel is the work equivalent of wearing a lanyard when you first get to college

Forum statistics

Threads
1,223,249
Messages
6,171,031
Members
452,374
Latest member
keccles

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top