如何在 SQL Server 中选择列名中带有特殊字符的列?

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

How to select a column in SQL Server with a special character in the column name?

sqlsql-servertsql

提问by Jin Yong

I have a table contain the '%' in the column title, and this cause problem when I do the select statement on that column (Find below for more details). Does anyone know how can I select that column by not keeping the original column title?

我有一个表在列标题中包含“%”,当我对该列执行 select 语句时,这会导致问题(在下面查找更多详细信息)。有谁知道如何通过不保留原始列标题来选择该列?

Example:

例子:

Table1
name  ref_no  tot_sales  %Phone
-------------------------------
Alan  1       1          100%
amy   2       1          50%
ken   3       4          30%

Script:

脚本:

Select %Phone From Table1

Error Message :

错误信息 :

Incorrect syntax near phone

手机附近的语法不正确

回答by Daniel Vassallo

You may want to wrap your column name in square brackets to have your identifier delimited:

您可能希望将列名括在方括号中以分隔标识符

SELECT [%Phone] FROM Table1

If the QUOTED_IDENTIFIERoption is set to ON, you can also use ANSI-SQL compliant double quotation marks to delimit identifiers:

如果该QUOTED_IDENTIFIER选项设置为 ON,您还可以使用符合 ANSI-SQL 的双引号来分隔标识符:

SELECT "%Phone" FROM Table1