I have a query that should request the top X number of records using the Qt framework to actually make the request to the SQL database. I have verified when I place an hard-coded number the query is successful, but when I attempt to bind to it I get an error.
query.prepare("SELECT TOP :numberToSelect"
" deviceId"
" , latitude"
" , longitude"
" , [timeStamp]"
" FROM Positions "
" WHERE [address] = ''"
" ORDER BY [timeStamp] DESC");
query.bindValue(":numberToSelect", numberMissing);
The variable numberMissing is an unsigned short which is passed in. Upon execution I receive this error:
Unable to execute statement: “[Microsoft][ODBC SQL Server Driver][SQL
Server]Incorrect syntax near ‘@P1’. [Microsoft][ODBC SQL Server
Driver][SQL Server]Statement(s) could not be prepared. QODBC3: Unable
to execute statement” “SELECT TOP ? deviceId , latitude ,
longitude , [timeStamp] FROM Positions WHERE [address] = ”
ORDER BY [timeStamp] DESC”
I do not see what the error would be.
When you do a select top with a variable, the top value needs to be in parenthesis.
Try this: