我的一个专栏叫。我不能改名字,因为不是我做的。 我是否允许做一些像SELECT from TableName或有一个特殊的语法,以避免SQL Server混淆?
当前回答
如果是在PostgreSQL中,在名字周围使用双引号,比如:
select "from" from "table";
注意:PostgreSQL内部自动将所有不带引号的命令和参数转换为小写。命令和标识符不区分大小写。sEleCt * from tAblE;解释为select * from table;。然而,双引号内的参数是原样使用的,因此是区分大小写的:select * from "table";and select * from "Table";从两个不同的表获取结果。
其他回答
我在尝试更新名称为关键字的列时遇到了同样的问题。上面的解决方案对我没有帮助。我通过简单地指定表的名称来解决它,就像这样:
UPDATE `survey`
SET survey.values='yes,no'
WHERE (question='Did you agree?')
将列名像这样用括号括起来,from变成[from]。
select [from] from table;
也可以使用以下命令(在查询多个表时很有用):
select table.[from] 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.
在表名上加上冒犯性的单词也可以。
我也遇到过这个问题。 解决方案是在查询中像这样放入[Column_Name]。
string query= "Select [Name],[Email] from Person";
所以它会工作得很好。
如果您正在使用SQL Server,您可以简单地将方括号括在列名或表名周围。
select [select]
from [table]
推荐文章
- 如何在Ruby On Rails中使用NuoDB手动执行SQL命令
- 查询JSON类型内的数组元素
- 确定记录是否存在的最快方法
- 获得PostgreSQL数据库中当前连接数的正确查询
- 在SQL选择语句Order By 1的目的是什么?
- 从现有模式生成表关系图(SQL Server)
- 我如何循环通过一组记录在SQL Server?
- 数据库和模式的区别
- 如何在SQL Server中一次更改多个列
- 如何从命令行通过mysql运行一个查询?
- 外键约束可能导致循环或多条级联路径?
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何选择每一行的列值不是独特的