Transact-SQL 語法慣例 (Transact-SQL)Transact-SQL Syntax Conventions (Transact-SQL)

適用於: 是SQL Server 是Azure SQL Database 是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. 每個能夠使用語法區塊的位置,都會以括在>形箭號內的標籤來指示:<標籤>。Each location in which the block of syntax could be used is indicated with the label enclosed in chevrons: <label>.

set 是運算式的集合,例如 <grouping set>。而 list 則是 set 的集合,例如 <composite element list>。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:


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

| schema_name.object_name| schema_name.object_name

| object_name| object_name

指定連結伺服器名稱或遠端伺服器名稱。Specifies a linked server name or remote server 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.

指定如果物件是在 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.

指向物件名稱。Refers to the name of the object.

當參考特定物件時,您不一定需要指定伺服器、資料庫和結構描述以供 SQL Server Database EngineSQL Server Database Engine 識別物件。When referencing a specific object, you don't always have to specify the server, database, and schema for the SQL Server Database EngineSQL 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.objectserver.database.schema.object 四部分名稱。Four-part name.
server.database..objectserver.database..object 省略結構描述名稱。Schema name is omitted.
server..schema.objectserver..schema.object 省略資料庫名稱。Database name is omitted.
server...objectserver...object 省略資料庫和結構描述名稱。Database and schema name are omitted.
database.schema.objectdatabase.schema.object 省略伺服器名稱。Server name is omitted.
database..objectdatabase..object 省略伺服器和結構描述名稱。Server and schema name are omitted.
schema.objectschema.object 省略伺服器和資料庫名稱。Server and database name are omitted.
objectobject 省略伺服器、資料庫和結構描述名稱。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:


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 Server (SQL Server 2008SQL Server 2008 及更新版本)、Azure SQL DatabaseAzure SQL DatabaseAzure Synapse Analytics (SQL 資料倉儲)Azure Synapse Analytics (SQL Data Warehouse) 相關的文章。The Transact-SQLTransact-SQL reference includes articles related to SQL ServerSQL Server (SQL Server 2008SQL Server 2008 and later), Azure SQL DatabaseAzure SQL Database, and Azure Synapse Analytics (SQL 資料倉儲)Azure Synapse Analytics (SQL Data Warehouse).

在每個文章頂端附近,都有一個章節指出支援該文章主題的產品。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 Server (SQL Server 2012 (11.x)SQL Server 2012 (11.x) 及更新版本),因為其不適用於 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) and later) 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). 您可以在任何 SQL ServerSQL Server 產品中使用 CREATE USER 陳述式,但 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 參考 (資料庫引擎41; 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