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

適用於: 是SQL Server (從 2008 開始) 是Azure SQL Database 是Azure SQL 資料倉儲 是平行處理資料倉儲 APPLIES TO: yesSQL Server (starting with 2008) yesAzure SQL Database yesAzure SQL Data Warehouse 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
UPPERCASEUPPERCASE Transact-SQLTransact-SQL 關鍵字。keywords.
斜體italic 使用者提供的 Transact-SQLTransact-SQL 語法參數。User-supplied parameters of Transact-SQLTransact-SQL syntax.
粗體字bold 必須完全依照顯示來輸入的資料庫名稱、資料表名稱、資料行名稱、索引名稱、預存程序、公用程式、資料類型名稱和文字。Database names, table names, column names, index names, stored procedures, utilities, data type names, and text that must be typed exactly as shown.
underlineunderline 指出省略陳述式中包含附加底線之值的子句時,所套用的預設值。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. 不要鍵入方括號。Do not type the brackets.
{ } (大括號){ } (braces) 必要的語法項目。Required syntax items. 不要鍵入大括號。Do not 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 Server]SQL Server 版本中大多數陳述式都不需要使用分號,但在未來的版本中將會需要。Although the semicolon is not required for most statements in this version of [SQL Server]SQL Server, it will be required in a future version.
<label> ::=<label> ::= 語法區塊的名稱。The name for a block of syntax. 這個慣例可用來分組與標示冗長語法的區段,或分組與標示可用於陳述式中之多個位置的語法單位。This convention is used to group and label sections of lengthy syntax or a unit of syntax that can be used in more than one location within a statement. 每個能夠使用語法區塊的位置,都使用括在>形箭號內的標籤來指示:<label>。Each location in which the block of syntax can 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:

server_name .[database_name].[schema_name].object_nameserver_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

指定連結伺服器名稱或遠端伺服器名稱。Specifies a linked server name or remote server name.

指定當物件在 [SQL Server]SQL Server 的本機執行個體中之 [SQL Server]SQL Server 資料庫的名稱。Specifies the name of a [SQL Server]SQL Server database when the object resides in a local instance of [SQL Server]SQL Server. 當物件是在連結伺服器中時,database_name 會指定一個 OLE DB 目錄。When the object is in a linked server, database_name specifies an OLE DB catalog.

指定如果物件是在 [SQL Server]SQL Server 資料庫中,包含物件的結構描述名稱。Specifies the name of the schema that contains the object if the object is in a [SQL Server]SQL 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 do not 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 cannot 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 DescriptionDescription
server .server . database .database . schema .schema . objectobject 四部分名稱。Four-part name.
server .server . database ..database .. objectobject 省略結構描述名稱。Schema name is omitted.
server ..server .. schema .schema . objectobject 省略資料庫名稱。Database name is omitted.
server ... objectserver ... object 省略資料庫和結構描述名稱。Database and schema name are omitted.
database .database . schema .schema . objectobject 省略伺服器名稱。Server name is omitted.
database ..database .. objectobject 省略伺服器和結構描述名稱。Server and schema name are omitted.
schema .schema . objectobject 省略伺服器和資料庫名稱。Server and database name are omitted.
objectobject 省略伺服器、資料庫和結構描述名稱。Server, database, and schema name are omitted.

程式碼範例慣例Code Example Conventions

除非另有指示,否則 Transact-SQLTransact-SQL 參考中所提供的範例都是利用 Transact-SQLSQL Server Management Studio 及其下列選項的預設值來測試的:Unless stated otherwise, the examples provided in the Transact-SQLTransact-SQL Reference were tested by using Transact-SQLSQL 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 Server]SQL Server ( SQL Server 2008SQL Server 2008SQL Server 2017SQL Server 2017)、 Azure SQL DatabaseAzure SQL DatabaseAzure SQL 資料倉儲Azure SQL Data Warehouse 的相關文章。The Transact-SQLTransact-SQL reference includes articles related to [SQL Server]SQL Server ( SQL Server 2008SQL Server 2008 through SQL Server 2017SQL Server 2017), Azure SQL DatabaseAzure SQL Database, and Azure SQL 資料倉儲Azure SQL Data Warehouse.

在每個文章頂端附近,都有一個小節指出適用於該文章主旨的產品。Near the top of each article is a section indicating which products support the subject of the article. 如果產品未被列出,表示該文章描述的功能不適用於該產品。If a product is omitted, then the feature described by the article is not 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 Server]SQL Server ( SQL Server 2012 (11.x)SQL Server 2012 (11.x)SQL Server 2017SQL Server 2017),因為它不適用於 SQL Server 2008SQL Server 2008SQL Server 2008 R2SQL Server 2008 R2Azure SQL DatabaseAzure SQL DatabaseThe CREATE AVAILABILITY GROUP article indicates it applies to [SQL Server]SQL Server ( SQL Server 2012 (11.x)SQL Server 2012 (11.x) through SQL Server 2017SQL Server 2017) because it does not apply to SQL Server 2008SQL Server 2008, SQL Server 2008 R2SQL Server 2008 R2, or Azure SQL DatabaseAzure SQL Database.

在某些情況下,某產品可用於一般文章主旨,但所有的引數卻都無法使用。In some cases, the general subject of the article can be used in a product, but all of the arguments are not supported. 例如自主資料庫使用者於 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 Server]SQL Server 產品中使用,但 WITH PASSWORD 語法就無法在舊版中使用。The CREATE USER statement can be used in any [SQL Server]SQL Server product, however the WITH PASSWORD syntax cannot be used with older versions. 在此情況下,就會在文章本文中於適當的引數描述中插入額外的<適用於>小節。In this case, 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