如何通过ODBC查询名称带有问号的MS Access列? [英] How to query for a MS Access column whose name has a question mark, via ODBC?
问题描述
我有一个MS Access数据库(用于记录的是Access 2002).它有一列,其名称包含一个问号,例如,表"Users"的列为"uid"和"isAdmin?".我需要通过ODBC连接到该数据库,并按照以下内容查询此列:
I have a MS Access database (Access 2002, for the record). It has a column whose name contains a question mark, say, for example table "Users" with columns "uid" and "isAdmin?". I need to connect to this database via ODBC, and query for this column, along the following lines:
select [uid], [isAdmin?] from Users order by [isAdmin?];
如何避开列名中的问号,以使MS Access ODBC驱动程序不认为这是查询参数?该查询不使用任何参数,因此,如果我完全禁用它们,就可以了.
How do I escape the question mark in the column name, so that the MS Access ODBC driver doesn't think that it's a query parameter? This query doesn't use any parameters, so it's fine if I disable them entirely.
一些限制:
- 我不能轻易更改列名.
- 我无法轻易地使用ODBC以外的其他东西进行连接,尽管如果我无法使ODBC正常运行,这可能是我的后备计划.
- 我不能只说
select * from Users
-它仍然会阻塞订单(在实际查询中这很复杂,因此确实需要在SQL中完成).
- I can't easily change the column names.
- I can't easily use something other than ODBC to connect, though this is probably my fallback plan if I can't get ODBC to behave.
- I can't just say
select * from Users
-- it'd still choke on the order by (which is complicated in the real query, so really needs to be done in SQL).
我尝试过的方法不起作用:
Things I've tried that don't work:
-
select [uid], '[isAdmin?]' from Users;
-这使第二列成为字符串"[isAdmin?]" -
select [uid], ['isAdmin?'] from Users;
-
select [uid], [isAdmin\?] from Users;
-
select [uid], [isAdmin\?] {escape '\'} from Users;
-其他任何转义字符也不起作用. -
select [uid], { [isAdmin?] } from Users;
select [uid], '[isAdmin?]' from Users;
-- this makes the second column be the string "[isAdmin?]"select [uid], ['isAdmin?'] from Users;
select [uid], [isAdmin\?] from Users;
select [uid], [isAdmin\?] {escape '\'} from Users;
-- nor does any other escape char work.select [uid], { [isAdmin?] } from Users;
编辑:我应该澄清一下,除了通过ODBC(或ADO或DAO或其他方式)外,我根本无法轻易更改数据库,但这会有些棘手,并且那时我可以通过这些命令运行查询.)
EDIT: I should have clarified, I can't easily change the database much at all, except via ODBC (or ADO or DAO or whatever, but that'll be a bit tricky, and at that point I can just run the query through those).
推荐答案
另一种选择是创建Users
表的视图并重命名该视图中的有问题的列.
Another option would be to create a view of the Users
table and rename the offending column in the view.
这篇关于如何通过ODBC查询名称带有问号的MS Access列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!