MS Access Error: The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect
asp.net, ms-access, sql
Solution
The N in TOP N queries in Jet SQL cannot be parameterized, period. You have to write your SQL string on the fly to get a variable N. This means either that you can't use a saved QueryDef, or that you have to edit the SQL of the QueryDef and save it before using it.
Problem
[Update: The query works if I hardcode in the parameters - so it has to do with the way I am adding parameters to the query] For the life of me, I cannot figure out what the problem is here. Here is the query being passed to the datareader: ``` SELECT * FROM (SELECT TOP ? StartDate, [ID] FROM (SELECT TOP ? StartDate, [ID] FROM Story ORDER BY StartDate DESC, [ID] DESC) AS foo ORDER BY StartDate ASC, [ID] ASC) AS bar INNER JOIN Story AS t ON bar.ID = t.ID ORDER BY bar.StartDate DESC, bar.[ID] DESC ``` The parameters are added in the following order: ``` var pNumToRetrieve = new OleDbParameter("", OleDbType.Integer) {Value = numToGet}; var pResultSet = new OleDbParameter("", OleDbType.Integer) {Value = resultSet}; _cmd.Parameters.Add(pNumToRetrieve); _cmd.Parameters.Add(pResultSet); ``` If I enter this query into access directly it works just fine. However, when running the query from ASP.NET, I get the following error: The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect. What am I doing wrong? Thanks, Adam