Transact-SQL 语法约定 (Transact-SQL)Transact-SQL Syntax Conventions (Transact-SQL)

适用对象: 是SQL Server 是Azure SQL 数据库 是Azure Synapse Analytics (SQL DW) 是并行数据仓库 APPLIES TO: yesSQL Server yesAzure SQL Database yesAzure Synapse Analytics (SQL DW) yesParallel Data Warehouse

下表列出了 Transact-SQLTransact-SQL 参考的语法关系图中使用的约定,并进行了说明。The following table lists and describes conventions that are used in the syntax diagrams in the Transact-SQLTransact-SQL Reference.

约定Convention 用于Used for
大写UPPERCASE Transact-SQLTransact-SQL 关键字。keywords.
斜体italic 用户提供的 Transact-SQLTransact-SQL 语法的参数。User-supplied parameters of Transact-SQLTransact-SQL syntax.
粗体bold 完全按显示原样键入数据库名称、表名称、列名、索引名称、存储过程、实用工具、数据类型名称和文本。Type database names, table names, column names, index names, stored procedures, utilities, data type names, and text exactly as shown.
下划线underline 指示当语句中省略了包含带下划线的值的子句时应用的默认值。Indicates the default value applied when the clause that contains the underlined value is omitted from the statement.
| (垂直条)| (vertical bar) 分隔括号或大括号中的语法项。Separates syntax items enclosed in brackets or braces. 只能使用其中一项。You can use only one of the items.
[ ](方括号)[ ] (brackets) 可选语法项。Optional syntax items. 不要键入方括号。Don't type the brackets.
{}(大括号){ } (braces) 必选语法项。Required syntax items. 不要键入大括号。Don't type the braces.
[ ...n][,...n] 指示前面的项可以重复 n 次。Indicates the preceding item can be repeated n number of times. 各项之间以逗号分隔。The occurrences are separated by commas.
[...n ][...n] 指示前面的项可以重复 n 次。Indicates the preceding item can be repeated n number of times. 每一项由空格分隔。The occurrences are separated by blanks.
;; Transact-SQLTransact-SQL 语句终止符。statement terminator. 虽然此版本的 SQL ServerSQL Server 中大部分语句都不需要分号,但今后发布的版本需要分号。Although the semicolon isn't required for most statements in this version of SQL ServerSQL Server, it will be required in a future version.
<label> ::=<label> ::= 语法块的名称。The name for a block of syntax. 使用此约定,可以对能在一条语句中的多个位置使用的过长语法段或语法单元进行分组和标记。Use this convention to group and label sections of lengthy syntax or a unit of syntax that you can use in more than one location within a statement. 可使用语法块的各个位置用括在尖括号内的标签指明:<label>。Each location in which the block of syntax could be used is indicated with the label enclosed in chevrons: <label>.

集是表达式的集合,例如 <分组集>;列表是集的集合,例如 <组合元素列表>。A set is a collection of expressions, for example <grouping set>; and a list is a collection of sets, for example <composite element list>.

多部分名称Multipart Names

除非另外指定,否则,所有对数据库对象名的 Transact-SQLTransact-SQL 引用将是由四部分组成的名称,格式如下:Unless specified otherwise, all Transact-SQLTransact-SQL references to the name of a database object can be a four-part name in the following form:

server_name .[database_name ].[schema_name ].object_name server_name.[database_name].[schema_name].object_name

| database_name .[schema_name ].object_name | database_name.[schema_name].object_name

| schema_name .object_name | schema_name.object_name

| object_name| object_name

server_nameserver_name
指定链接的服务器名称或远程服务器名称。Specifies a linked server name or remote server name.

database_name database_name
如果对象驻留在 SQL ServerSQL Server 的本地实例中,则指定 SQL ServerSQL Server 数据库的名称。Specifies the name of a SQL ServerSQL Server database when the object resides in a local instance of SQL ServerSQL Server. 如果对象在链接服务器中,则 database_name 将指定 OLE DB 目录 。When the object is in a linked server, database_name specifies an OLE DB catalog.

schema_name schema_name
如果对象在 SQL ServerSQL Server 数据库中,则指定包含对象的架构的名称。Specifies the name of the schema that contains the object if the object is in a SQL ServerSQL Server database. 如果对象在链接服务器中,则 schema_name 将指定 OLE DB 架构名称 。When the object is in a linked server, schema_name specifies an OLE DB schema name.

object_name object_name
对象的名称。Refers to the name of the object.

引用某个特定对象时,不必总是指定服务器、数据库和架构,SQL Server 数据库引擎SQL Server Database Engine 也能标识对象。When referencing a specific object, you don't always have to specify the server, database, and schema for the SQL Server 数据库引擎SQL Server Database Engine to identify the object. 不过,如果找不到对象,便会返回错误。However, if the object can't be found, an error is returned.

备注

为了避免名称解析错误,建议只要指定了架构范围内的对象时就指定架构名称。To avoid name resolution errors, we recommend specifying the schema name whenever you specify a schema-scoped object.

若要省略中间节点,请使用句点来指示这些位置。To omit intermediate nodes, use periods to indicate these positions. 下表显示了对象名的有效格式。The following table shows the valid formats of object names.

对象引用格式Object reference format 描述Description
server .database .schema .object server.database.schema.object 四个部分的名称。Four-part name.
server .database ..object server.database..object 省略架构名称。Schema name is omitted.
server ..schema .object server..schema.object 省略数据库名称。Database name is omitted.
server ...object server...object 省略数据库和架构名称。Database and schema name are omitted.
database .schema .object database.schema.object 省略服务器名。Server name is omitted.
database ..object database..object 省略服务器和架构名称。Server and schema name are omitted.
schema .object schema.object 省略服务器和数据库名称。Server and database name are omitted.
对象 object 省略服务器、数据库和架构名称。Server, database, and schema name are omitted.

代码示例约定Code Example Conventions

除非专门说明,否则,在 Transact-SQLTransact-SQL 参考中提供的示例都已使用 SQL Server Management StudioSQL Server Management Studio 及其以下选项的默认设置进行了测试:Unless stated otherwise, the examples provided in the Transact-SQLTransact-SQL Reference were tested by using SQL Server Management StudioSQL Server Management Studio and its default settings for the following options:

  • ANSI_NULLSANSI_NULLS
  • ANSI_NULL_DFLT_ONANSI_NULL_DFLT_ON
  • ANSI_PADDINGANSI_PADDING
  • ANSI_WARNINGSANSI_WARNINGS
  • CONCAT_NULL_YIELDS_NULLCONCAT_NULL_YIELDS_NULL
  • QUOTED_IDENTIFIERQUOTED_IDENTIFIER

Transact-SQLTransact-SQL 参考中的大多数代码示例都已在运行区分大小写排序顺序的服务器上进行了测试。Most code examples in the Transact-SQLTransact-SQL Reference have been tested on servers that are running a case-sensitive sort order. 测试服务器通常运行 ANSI/ISO 1252 代码页。The test servers were typically running the ANSI/ISO 1252 code page.

许多代码示例用字母 N 作为 Unicode 字符串常量的前缀 。如果没有 N 前缀,则字符串被转换为数据库的默认代码页 。Many code examples prefix Unicode character string constants with the letter N. Without the N prefix, the string is converted to the default code page of the database. 此默认代码页可能不识别某些字符。This default code page may not recognize certain characters.

“适用于”引用"Applies to" References

Transact-SQLTransact-SQL 参考包括与 SQL ServerSQL ServerSQL Server 2008SQL Server 2008SQL ServerSQL Server)、Azure SQL DatabaseAzure SQL DatabaseAzure Synapse Analytics (SQL DW)Azure Synapse Analytics (SQL DW) 相关的文章。The Transact-SQLTransact-SQL reference includes articles related to SQL ServerSQL Server (SQL Server 2008SQL Server 2008 through SQL ServerSQL Server), Azure SQL DatabaseAzure SQL Database, and Azure Synapse Analytics (SQL DW)Azure Synapse Analytics (SQL DW).

每篇文章的顶部附近有指明哪些产品支持文章主题的部分。There's a section near the top of each article indicating which products support the article's subject. 如果省略了某产品,文章描述的功能就不适用于此产品。If a product is omitted, then the feature described by the article isn't available in that product. 例如,在 SQL Server 2012 (11.x)SQL Server 2012 (11.x)中介绍了可用性组。For example, availability groups were introduced in SQL Server 2012 (11.x)SQL Server 2012 (11.x). “CREATE AVAILABILITY GROUP” 一文指明它适用于 SQL ServerSQL ServerSQL Server 2012 (11.x)SQL Server 2012 (11.x) 一直到 SQL ServerSQL Server),因为它不适用于 SQL Server 2008SQL Server 2008SQL Server 2008 R2SQL Server 2008 R2Azure SQL DatabaseAzure SQL DatabaseThe CREATE AVAILABILITY GROUP article indicates it applies to SQL ServerSQL Server (SQL Server 2012 (11.x)SQL Server 2012 (11.x) through SQL ServerSQL Server) because it doesn't apply to SQL Server 2008SQL Server 2008, SQL Server 2008 R2SQL Server 2008 R2, or Azure SQL DatabaseAzure SQL Database.

文章的常规主题可能用于某一产品,但在某些情况下,所有参数都不受支持。The general subject of the article might be used in a product, but all of the arguments aren't supported in some cases. 例如,在 SQL Server 2012 (11.x)SQL Server 2012 (11.x)中介绍了包含的数据库用户。For example, contained database users were introduced in SQL Server 2012 (11.x)SQL Server 2012 (11.x). CREATE USER 语句可用于任何 SQL ServerSQL Server 产品,但 WITH PASSWORD 语法无法用于旧版本。Use the CREATE USER statement in any SQL ServerSQL Server product, however the WITH PASSWORD syntax can't be used with older versions. 其他“适用于” 部分插入到文章正文中的相应参数说明中。Additional Applies to sections are inserted into the appropriate argument descriptions in the body of the article.

另请参阅See Also

Transact-SQL 参考(数据库引擎) Transact-SQL Reference (Database Engine)
保留关键字 (Transact SQL) Reserved Keywords (Transact SQL)
Transact-SQL 设计问题 Transact-SQL Design Issues
Transact-SQL 命名问题 Transact-SQL Naming Issues
Transact-SQL 性能问题Transact-SQL Performance Issues