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


当前回答

一些可靠的答案——但投票最多的答案是狭隘的,只涉及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.

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

其他回答

下面的方法会很有效:

SELECT DISTINCT table.from AS a FROM table

我在尝试更新名称为关键字的列时遇到了同样的问题。上面的解决方案对我没有帮助。我通过简单地指定表的名称来解决它,就像这样:

UPDATE `survey`
SET survey.values='yes,no'
WHERE (question='Did you agree?')

在Apache Drill中,使用反引号:

select `from` from table;

简单的解决方案

假设列名是from;因此,查询中的列名可以通过表别名引用

Select * from user u where u.from="US"

在Oracle SQL Developer中,pl/ SQL可以使用双引号,但如果使用双引号,则必须以大写字母输入列名。例如,从MY_TABLE中选择“FROM”