0

I am trying to run the query below with parameter and keep getting the error message,

select distinct count( distinct x.can) 
FROM dbo.tblxenon as x
WHERE x.date >= ?  and x.date <= ? AND x.acc like 'NT%'

When I type in the dates the query runs fine.

When I change the first date, i.e. x.date >= , to a ? with a typed in date in the second date, i.e x.date <=, the query runs fine.

However, when I change the second date to a ? I get the error message 'The multipart identifier x.date could not be bound.

What am I doing wrong?

Thanking you in advance, guys.

Edit

This is the code with the dates typed in

select distinct count( distinct x.can)
FROM dbo.tblxenon as x
WHERE x.date >= '01/04/2016'  and x.date <= '30/04/2016' AND x.acc like 'NT%'

The query runs just fine like this.

When I replace the dates with ? to pass parameters, I get the error.

Kiki
  • 1
  • 1
  • First of all try `x.[date]` – Giorgi Nakeuri May 17 '16 at 10:12
  • 3
    Instead of "change X, change Y, change Z", can you show the non-working query? – CodeCaster May 17 '16 at 10:13
  • @GiorgiNakeuri I don't think SQL-Server has a problem with reserved word when they are specified after an alias. – sagi May 17 '16 at 10:20
  • Welcome to stackoverflow. Please read [ask]. – Zohar Peled May 17 '16 at 11:30
  • Hi @CodeCaster. The non- working query is the one shown above. – Kiki May 17 '16 at 11:32
  • That doesn't really clear it up. You can't run a query containing question marks, that's placeholder syntax for parameterized queries. Why would you expect that to work? – CodeCaster May 17 '16 at 11:33
  • @CodeCaster, when I type in the dates i.e. replace the question marks with actual dates, the query runs fine. The issue is when I replace the dates with the question marks to add/pass parameters to the query. – Kiki May 17 '16 at 14:29
  • Please show the code you use this query in and how you pass the parameters. – CodeCaster May 17 '16 at 14:30
  • @CodeCaster, Hi I have amended my question accordingly – Kiki May 17 '16 at 14:43
  • Come on. **Show the code where you replace the question marks and pass the parameters**. Where did you get the idea to use question marks? Which API and provider are you using for querying? Hint: question marks are for ODBC, you probably just need named parameters. But this cannot be answered if you don't show any relevant code. – CodeCaster May 17 '16 at 14:43
  • @CodeCaster, Sorry I am somewhat of a novice, I dont understand what you are asking me. The error message reads [Microsoft][ODBC SQL Server Driver][SQL Server]The multi-part identifier 'x.date' could not be bound. I dont know if this provides any clarity. – Kiki May 17 '16 at 15:35
  • I'm basically asking where and how you use this SQL. – CodeCaster May 17 '16 at 15:38
  • 1
    @CodeCaster, I use this database query in an excel spreadsheet. The parameters I am trying to pass are dates in cells within the spreadsheet. – Kiki May 17 '16 at 15:45
  • Alright, thank you for that clarification. Please [edit] that last comment into your question. This question might be relevant: [Excel: Use a cell value as a parameter for a SQL query](http://stackoverflow.com/questions/1285686/excel-use-a-cell-value-as-a-parameter-for-a-sql-query). – CodeCaster May 17 '16 at 20:10

0 Answers0