我的一个专栏叫。我不能改名字,因为不是我做的。 我是否允许做一些像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.

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

其他回答

我也遇到过这个问题。 解决方案是在查询中像这样放入[Column_Name]。

string query= "Select [Name],[Email] from Person";

所以它会工作得很好。

当你这样做的时候,把它别名为其他东西(或者更好的是,使用一个视图或SP并弃用旧的直接访问方法)。

SELECT [from] AS TransferFrom -- Or something else more suitable
FROM TableName

嗨,我在完全符合ANSI标准的Teradata系统上工作。使用双引号“”来命名这样的列。

例如,type是一个SQL保留关键字,当在引号中使用时,type被视为用户指定的名称。

参见下面的代码示例:

CREATE TABLE alpha1
AS
(
SEL
product1
type_of_product AS "type"
FROM beta1
) WITH DATA
PRIMARY INDEX (product1)

--type is a SQL reserved keyword

TYPE

--see? now to retrieve the column you would use:

SEL "type" FROM alpha1

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

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

简单的解决方案

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

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