SQL sql语句中的[]括号

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

[] brackets in sql statements

sqlms-access

提问by Adam Lerman

What do the brackets do in a sql statement?

括号在sql语句中的作用是什么?

For example, in the statement:

例如,在声明中:

insert into table1 ([columnname1], columnname2) values (val1, val2)

Also, what does it do if the table name is in brackets?

另外,如果表名在括号中,它会做什么?

回答by albertein

The [] marks the delimitation of a identifier, so if you have a column whose name contains spaces like Order Qty you need to enclose it with [] like:

[] 标记标识符的定界,因此如果您有一列名称包含像 Order Qty 这样的空格,您需要用 [] 将其括起来,例如:

select [Order qty] from [Client sales]

They are also to escape reserved keywords used as identifiers

它们还用于转义用作标识符的保留关键字

回答by Bill Karwin

This is Microsoft SQL Server nonstandard syntax for "delimited identifiers." SQL supports delimiters for identifiers to allow table names, column names, or other metadata objects to contain the following:

这是用于“分隔标识符”的 Microsoft SQL Server 非标准语法。SQL 支持标识符的分隔符,以允许表名、列名或其他元数据对象包含以下内容:

  • SQL reserved words: "Order"
  • Words containing spaces: "Order qty"
  • Words containing punctuation: "Order-qty"
  • Words containing international characters
  • Column names that are case-sensitive: "Order" vs. "order"
  • SQL 保留字:“订单”
  • 包含空格的单词:“订购数量”
  • 含有标点符号的词语:“订单数量”
  • 包含国际字符的单词
  • 区分大小写的列名:“订单”与“订单”

Microsoft SQL Server uses the square brackets, but this is not the syntax standard SQL uses for delimited identifiers. Standardly, double-quotes should be used for delimiters.

Microsoft SQL Server 使用方括号,但这不是 SQL 用于分隔标识符的语法标准。标准情况下,应该使用双引号作为分隔符。

In Microsoft SQL Server, you can enable a mode to use standard double-quotes for delimiters as follows:

在 Microsoft SQL Server 中,您可以启用使用标准双引号作为分隔符的模式,如下所示:

SET QUOTED_IDENTIFIER ON;

回答by Jorge Ferreira

They are meant to escape reserved keywords or invalid column identifiers.

它们旨在转义保留关键字或无效的列标识符。

CREATE TABLE test
(
  [select] varchar(15)
)

INSERT INTO test VALUES('abc')

SELECT [select] FROM test

回答by Orion Adrian

Anything inside the brackets is considered a single identifier (e.g. [test machine]. This can be used to enclose names with spaces or to escape reserve words (e.g. [order], [select], [group]).

括号内的任何内容都被视为单个标识符(例如 [test machine]。这可用于用空格括起名称或转义保留字(例如 [order]、[select]、[group])。

回答by Ty.

They allow you to use keywords (such as date) in the name of the column, table, etc...

它们允许您在列、表等的名称中使用关键字(例如date)...

Since this is a bad practice to begin with, they are generally not included. The only place you should see them being used is by people starting out with sql queries that don't know any better. Other than that they just clutter up your query.

由于这是一种不好的做法,因此通常不包括在内。您应该看到它们被使用的唯一地方是人们开始使用不知道更好的 sql 查询。除此之外,它们只会使您的查询变得混乱。

回答by sumon

if you use any column name which is same as any reserved keyword in sql, in that case you can put the column name in square bracket to distinguish between your custom column name and existing reserved keyword.

如果您使用与 sql 中的任何保留关键字相同的任何列名,在这种情况下,您可以将列名放在方括号中以区分自定义列名和现有保留关键字。

回答by juFo

When having table names or filenames with spaces or dashes (-) etc... you can receive "Systax error in FROM clause". Use [] brackets to solve this.

当表名或文件名带有空格或破折号 (-) 等时...您可能会收到“FROM 子句中的系统税错误”。使用 [] 括号来解决这个问题。

See: https://msdn.microsoft.com/en-us/library/ms175874.aspx

请参阅:https: //msdn.microsoft.com/en-us/library/ms175874.aspx

回答by Darrel Miller

They are simply delimiters that allow you to put special characters (like spaces) in the column or table name e.g.

它们只是允许您在列名或表名中放置特殊字符(如空格)的分隔符,例如

insert into [Table One] ([Column Name 1], columnname2) values (val1, val2)