我的一个专栏叫。我不能改名字,因为不是我做的。 我是否允许做一些像SELECT from TableName或有一个特殊的语法,以避免SQL Server混淆?


当前回答

将列名像这样用括号括起来,from变成[from]。

select [from] from table;

也可以使用以下命令(在查询多个表时很有用):

select table.[from] from table;

其他回答

你可以像这样把你的列名放在括号里:

Select  [from] from < ur_tablename>

Or

放入一个临时表,然后随意使用。 例子:

Declare @temp_table table(temp_from varchar(max))

Insert into @temp_table
Select * from your_tablename

这里我只是假设your_tablename只包含一列(即from)。

将列名像这样用括号括起来,from变成[from]。

select [from] from table;

也可以使用以下命令(在查询多个表时很有用):

select table.[from] from table;

从这里的答案和我自己的经验来看。如果您计划实现可移植,唯一可接受的答案是不要对表、列或其他名称使用SQL关键字。

所有这些答案都适用于各种数据库,但显然有很多不支持ANSI解决方案。

如果您正在使用SQL Server,您可以简单地将方括号括在列名或表名周围。

select [select]
from [table]

一些可靠的答案——但投票最多的答案是狭隘的,只涉及SQL Server。总而言之:

If you have source control, the best solution is to stick to the rules, and avoid using reserved words. This list has been around for ages, and covers most of the peculiarities. One tip is that reserved words are rarely plural—so you're usually safe using plural names. Exceptions are DIAGNOSTICS, SCHEMAS, OCTETS, OFFSETS, OPTIONS, VALUES, PARAMETERS, PRIVILEGES and also verb-like words that also appear plural: OVERLAPS, READS, RETURNS, TRANSFORMS. Many of us don't have the luxury of changing the field names. There, you'll need to know the details of the RDBM you're accessing: For SQL Server use [square_braces] around the name. This works in an ODBC connection too. For MySQL use `back_ticks`. Postgres, Oracle and several other RDBMs will apparently allow "double_quotes" to be used.

在表名上加上冒犯性的单词也可以。