ODBC SQL, Using a reserved word in a where clause

半世苍凉 提交于 2019-12-12 02:53:32

问题


I have a problem where I need to search for a value in a ODBC which is a reserved word. So:

SELECT * FROM Customer WHERE AccountNumber = 'Sum'

In this case, 'Sum' is a reserved word and so I get a syntax error. In access the error is "The SELECT statement includes a reserved word or an argument name that is misspelled or missing, or the punctuation is incorrect"

Is there a way I can search for such a string?


回答1:


Parameterise the string you wish to use as part of the WHERE clause and as I had the same problem when using the keyword 'PRIORITY'. After modifying the C# code to use parameters, it worked.

using (OdbcCommand command = conn.CreateCommand())
{
   command.CommandText = "SELECT account_ref FROM SALES_LEDGER WHERE account_ref = '?'";
   command.Parameters.Add(new OdbcParameter("?", accountCode));

   using (OdbcDataReader reader = command.ExecuteReader())
   {
      ...
   }
}



回答2:


Instead of searching for 'Sum' specifically. You could try:

SELECT * FROM Customer WHERE AccountNumber LIKE('%Sum%')


来源:https://stackoverflow.com/questions/18830139/odbc-sql-using-a-reserved-word-in-a-where-clause

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!