SQL 查询语法错误 - 字段名称中的空格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7984124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SQL Query Syntax Error - Spaces in Field Names
提问by Jim Fell
The database my application uses has field names containing spaces. I believe this to be the cause of my problem. Here is a typical query:
我的应用程序使用的数据库具有包含空格的字段名称。我相信这是我的问题的原因。这是一个典型的查询:
SELECT * FROM 'OV2 BAS' AS bas
INNER JOIN 'OV2 RefID' AS ids ON 'bas.Ref ID' = 'ids.Ref ID'
WHERE ids.ENUM_H = 'TDischarge';
How do I deal with the spaces in the field names? Thanks.
如何处理字段名称中的空格?谢谢。
Additional Information
附加信息
This is to access a database made with MS Access 2007 (Microsoft.ACE.OLEDB.12.0).
这是为了访问使用 MS Access 2007 (Microsoft.ACE.OLEDB.12.0) 制作的数据库。
回答by James Johnson
I don't think you can use quotes around the actual table name; only the name you assign to it. I would wrap the table in brackets instead: [OV2 BAS]
我认为您不能在实际表名周围使用引号;只有您分配给它的名称。我会将表格括在括号中:[OV2 BAS]
You also can't put quotes around your joining syntax either. Try this instead:
您也不能在连接语法周围加上引号。试试这个:
SELECT * FROM [OV2 BAS] AS bas INNER JOIN [OV2 RefID] AS ids ON bas.[Ref ID] = ids.[Ref ID] WHERE ids.ENUM_H = 'TDischarge';
回答by Branko Dimitrijevic
Replace '
with
替换'
为
- postgreSQL, Oracle:
"
- MySQL
`
- SQL-server:
[
and]
- PostgreSQL,甲骨文:
"
- MySQL
`
- SQL服务器:
[
和]
For example: "OV2 BAS"
, bas."Ref ID" = ids."Ref ID"
, etc.
例如:"OV2 BAS"
,bas."Ref ID" = ids."Ref ID"
,等。
回答by Rob Bowman
For Microsoft Access, wrap field names that contain spaces with back ticks e.g. SELECT `Eng Units` FROM Table
对于 Microsoft Access,将包含空格的字段名称用反勾号换行,例如 SELECT `Eng Units` FROM Table
回答by Christian Specht
That depends on the database engine you're using.
For SQL Server, you have to put the field names in brackets: [ ]
这取决于您使用的数据库引擎。
对于 SQL Server,您必须将字段名称放在括号中:[ ]
SELECT * FROM [OV2 BAS] AS bas
INNER JOIN [OV2 RefID] AS ids ON bas.[Ref ID] = ids.[Ref ID]
WHERE ids.ENUM_H = 'TDischarge';
回答by Marc B
You don't specify which DBMS you're using, but I'm guessing SQL server, so
您没有指定您使用的是哪个 DBMS,但我猜是 SQL 服务器,所以
SELECT *
FROM [OV2 BAS] AS bas
^^^^^^^^^
... enclose the field name in brackets. Using quotes as you are, turns the field name into a plain string, which will NOT be treated as a fieldname by SQL server.
... 将字段名称括在方括号中。按原样使用引号将字段名称转换为纯字符串,SQL 服务器不会将其视为字段名称。