SQL 如何在SELECT子句中用点(“.”)编写列名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5634501/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 10:07:14  来源:igfitidea点击:

How to write a column name with dot (".") in the SELECT clause?

sqlsql-server-2008

提问by Liran Ben Yehuda

I'm trying to write a column name using "." with no success

我正在尝试使用“.”编写列名。没有成功

sample:

样本:

SELECT PrmTable.Value = MAX(Value)
FROM TempTable

or

或者

SELECT MAX(Value) AS PrmTable.Value
FROM TempTable

Any idea ?

任何的想法 ?

回答by AdaTheDev

Just enclose it in square brackets, and it will work

只需将它括在方括号中,它就会起作用

e.g.

例如

SELECT MAX(Value) AS [PrmTable.Value]
FROM TempTable

回答by Tony

I would not recommend you use field names which always require you to enclose the name in brackets, it becomes a pain.

我不建议您使用总是要求您将名称括在括号中的字段名称,这会变得很痛苦。

Also the period is used in SQL Server to denote schema and database name separators. Using your field name the full name for a field becomes:

SQL Server 中也使用句点来表示架构和数据库名称分隔符。使用您的字段名称,字段的全名变为:

[DatabaseName].[SchemaName].[TableName].[FieldName.WithPeriod]

That just looks odd and would probably confuse other DBAs. Use an underscore to separate words in your field names, it's a much more common style:

这看起来很奇怪,可能会混淆其他 DBA。使用下划线分隔字段名称中的单词,这是一种更常见的样式:

[DatabaseName].[SchemaName].[TableName].[FieldName_WithUnderscore]

回答by Akram Shahda

SELECT [PrmTable.Value] = MAX(Value)

FROM TempTable

or

SELECT MAX(Value) AS [PrmTable.Value]