ODBC SQL, Using a reserved word in a where clause

2019-09-03 09:01发布

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?

2条回答
仙女界的扛把子
2楼-- · 2019-09-03 09:25

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())
   {
      ...
   }
}
查看更多
The star\"
3楼-- · 2019-09-03 09:38

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

SELECT * FROM Customer WHERE AccountNumber LIKE('%Sum%')
查看更多
登录 后发表回答