TOP (Transact-SQL)TOP (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

SQL Server 2019 (15.x)SQL Server 2019 (15.x) 中,將查詢結果集中所傳回的資料列數限制為指定數目的資料列或是資料列的百分比。Limits the rows returned in a query result set to a specified number of rows or percentage of rows in SQL Server 2019 (15.x)SQL Server 2019 (15.x). 當您搭配 ORDER BY 子句使用 TOP 時,結果集會限制為前 N 個已排序資料列。When you use TOP with the ORDER BY clause, the result set is limited to the first N number of ordered rows. 否則,TOP 會以未定義的順序傳回前 N 個資料列。Otherwise, TOP returns the first N number of rows in an undefined order. 請使用此子句來指定 SELECT 陳述式所傳回的資料列數目。Use this clause to specify the number of rows returned from a SELECT statement. 或者,使用 TOP 來指定 INSERT、UPDATE、MERGE 或 DELETE 陳述式所影響的資料列。Or, use TOP to specify the rows affected by an INSERT, UPDATE, MERGE, or DELETE statement.

主題連結圖示 Transact-SQL 語法慣例Topic link icon Transact-SQL Syntax Conventions

語法Syntax

下列是 SQL Server 和 Azure SQL Database 的語法:Following is the syntax for SQL Server and Azure SQL Database:

[   
    TOP (expression) [PERCENT]  
    [ WITH TIES ]  
]  

下列是 Azure SQL 資料倉儲和平行處理資料倉儲的語法:Following is syntax for Azure SQL Data Warehouse and Parallel Data Warehouse:

[   
    TOP ( expression )   
    [ WITH TIES ]  
]  

引數Arguments

expressionexpression
指定所要傳回資料列數目的數值運算式。The numeric expression that specifies the number of rows to be returned. 如果您指定了 PERCENT,expression 會隱含地轉換為 float 值。expression is implicitly converted to a float value if you specify PERCENT. 否則,expression 會轉換為 bigintOtherwise, expression is converted to bigint.

PERCENTPERCENT
指出查詢只從結果集中傳回前 expression % 的資料列。Indicates that the query returns only the first expression percent of rows from the result set. 含小數的值會無條件進位到下一個整數值。Fractional values are rounded up to the next integer value.

WITH TIESWITH TIES
傳回針對限制結果集中的最後一個位數繫結的兩個或多個資料列。Returns two or more rows that tie for last place in the limited results set. 您必須搭配 ORDER BY 子句使用這個引數。You must use this argument with the ORDER BY clause. WITH TIES 可能會導致傳回比 expression 中所指定值更多的資料列。WITH TIES might cause more rows to be returned than the value specified in expression. 例如,若 expression 設定為 5,但兩個額外資料列符合資料列 5 中 ORDER BY 資料行的值,結果集就會包含七個資料列。For example, if expression is set to 5 but two additional rows match the values of the ORDER BY columns in row 5, the result set will contain seven rows.

只限於 SELECT 陳述式中,且只有在您也指定了 ORDER BY 子句時,才能搭配 WITH TIES 引數指定 TOP 子句。You can specify the TOP clause with the WITH TIES argument only in SELECT statements, and only if you've also specified the ORDER BY clause. 有同值記錄的傳回順序是任意的。The returned order of tying records is arbitrary. ORDER BY 不會影響此規則。ORDER BY doesn't affect this rule.

最佳做法Best Practices

在 SELECT 陳述式中,永遠搭配 TOP 子句使用 ORDER BY 子句。In a SELECT statement, always use an ORDER BY clause with the TOP clause. 因為它是以預測方式來指示哪些資料列受到 TOP 影響的唯一方法。Because, it's the only way to predictably indicate which rows are affected by TOP.

在 ORDER BY 子句中使用 OFFSET 和 FETCH 子句 (而不要使用 TOP 子句),來實作查詢分頁方案。Use OFFSET and FETCH in the ORDER BY clause instead of the TOP clause to implement a query paging solution. 使用 OFFSET 和 FETCH 子句,比較容易實作分頁方案 (也就是將資料區塊或「頁面」傳送到用戶端)。A paging solution (that is, sending chunks or "pages" of data to the client) is easier to implement using OFFSET and FETCH clauses. 如需詳細資訊,請參閱 ORDER BY 子句 (Transact-SQL)For more information, see ORDER BY Clause (Transact-SQL).

使用 TOP 或 OFFSET 和 FETCH (而不要使用 SET ROWCOUNT) 來限制傳回的資料列數目。Use TOP (or OFFSET and FETCH) instead of SET ROWCOUNT to limit the number of rows returned. 這些方法優於 SET ROWCOUNT 用法,原因如下:These methods are preferred over using SET ROWCOUNT for the following reasons:

  • 在 SELECT 陳述式中,查詢最佳化工具在查詢最佳化期間會考量 TOP 或 FETCH 子句中的 expression 值。As a part of a SELECT statement, the query optimizer can consider the value of expression in the TOP or FETCH clauses during query optimization. 因為您是在執行查詢的陳述式之外使用 SET ROWCOUNT,所以它的值不能在查詢計劃中列入考量。Because you use SET ROWCOUNT outside of a statement that runs a query, its value can't be considered in a query plan.

相容性支援Compatibility Support

基於回溯相容性,括號在 SELECT 陳述式中是選擇性。For backward compatibility, the parentheses are optional in SELECT statements. 建議您在 SELECT 陳述式中一律為 TOP 使用括弧。We recommend that you always use parentheses for TOP in SELECT statements. 這麼做可以與 INSERT、UPDATE、MERGE 和 DELETE 陳述式中的必要括弧用法保持一致性。Doing so provides consistency with its required use in INSERT, UPDATE, MERGE, and DELETE statements.

互通性Interoperability

TOP 運算式不會影響因觸發程序而執行的陳述式。The TOP expression doesn't affect statements that might run because of a trigger. 在觸發程序中,inserteddeleted 資料表只會傳回真正受 INSERT、UPDATE、MERGE 或 DELETE 陳述式影響的資料列。The inserted and deleted tables in the triggers return only the rows that are truly affected by the INSERT, UPDATE, MERGE, or DELETE statements. 例如,若因使用 TOP 子句的 INSERT 陳述式而引發 INSERT TRIGGER,For example, if an INSERT TRIGGER fires as the result of an INSERT statement that used a TOP clause.

SQL ServerSQL Server 允許透過檢視表更新資料列。allows for updating rows through views. 由於您可以在檢視定義中包含 TOP 子句;因此,如果資料列因更新而不再符合 TOP 運算式的需求,則部分資料列可能會在檢視表中消失。Because you can include the TOP clause in the view definition, certain rows may disappear from the view if the rows no longer meet the requirements of the TOP expression due to an update.

在 MERGE 陳述式中指定時,TOP 子句會在整個來源資料表和整個目標資料表聯結在一起「之後」 套用。When specified in the MERGE statement, the TOP clause applies after the entire source table and the entire target table are joined. 並且,將會移除不符合插入、更新或刪除動作的聯結資料列。And, the joined rows that don't qualify for an insert, update, or delete action are removed. TOP 子句會進一步將聯結資料列的列數減少為指定的值,且插入、更新或刪除動作會依照未排序方式套用到剩餘的聯結資料列。The TOP clause further reduces the number of joined rows to the specified value and the insert, update, or delete actions apply to the remaining joined rows in an unordered way. 亦即,將資料列散發到 WHEN 子句中定義的動作時,沒有任何特定順序。That is, there's no order in which the rows are distributed among the actions defined in the WHEN clauses. 例如,若指定 TOP (10) 會影響 10 個資料列,則在這些資料列中,可能會更新七個及插入三個。For example, if specifying TOP (10) affects 10 rows, of these rows, seven may be updated and three inserted. 或者,可能會刪除一個、更新五個及插入四個等等。Or, one may be deleted, five updated, and four inserted, and so on. 因為 MERGE 陳述式會針對來源和目標資料表執行完整資料表掃描,所以當您使用 TOP 子句以藉由建立多個批次來修改大型資料表時,I/O 效能可能會受到影響。Because the MERGE statement does a full table scan of both the source and target tables, I/O performance can be affected when you use the TOP clause to modify a large table by creating multiple batches. 在此狀況中,請務必確保所有後續批次都以新的資料列為目標。In this scenario, it's important to ensure that all successive batches target new rows.

當您在查詢中指定包含 UNION、UNION ALL、EXCEPT 或 INTERSECT 運算子的 TOP 子句時,請特別小心。Use caution when you're specifying the TOP clause in a query that contains a UNION, UNION ALL, EXCEPT, or INTERSECT operator. 您可能撰寫一個傳回非預期結果的查詢,因為當這些運算子用於選取作業時,TOP 和 ORDER BY 子句的邏輯處理順序不一定是直覺式。It's possible to write a query that returns unexpected results because the order in which the TOP and ORDER BY clauses are logically processed isn't always intuitive when these operators are used in a select operation. 例如,提供下列資料表和資料時,假設您想要傳回最便宜的紅色汽車和最便宜的藍色汽車。For example, given the following table and data, assume that you want to return the least expensive red car and the least expensive blue car. 也就是紅色轎車和藍色小貨車。That is, the red sedan and the blue van.

CREATE TABLE dbo.Cars(Model varchar(15), Price money, Color varchar(10));  
INSERT dbo.Cars VALUES  
    ('sedan', 10000, 'red'), ('convertible', 15000, 'blue'),   
    ('coupe', 20000, 'red'), ('van', 8000, 'blue');  

若要達成這些結果,您可能撰寫下列查詢。To achieve these results, you might write the following query.

SELECT TOP(1) Model, Color, Price  
FROM dbo.Cars  
WHERE Color = 'red'  
UNION ALL  
SELECT TOP(1) Model, Color, Price  
FROM dbo.Cars  
WHERE Color = 'blue'  
ORDER BY Price ASC;  
GO    

下列為結果集。Following is the result set.

Model         Color      Price  
------------- ---------- -------  
sedan         red        10000.00  
convertible   blue       15000.00

傳回未預期的結果,因為 TOP 子句邏輯上在 ORDER BY 子句之前執行,這樣會排序運算子 (在此例中為 UNION ALL) 的結果。The unexpected results are returned because the TOP clause logically runs before the ORDER BY clause, which sorts the results of the operator (UNION ALL in this case). 因此,上一個查詢會傳回任何一輛紅色汽車和任何一輛藍色汽車,然後依價格來排序該聯集的結果。So, the previous query returns any one red car and any one blue car and then orders the result of that union by the price. 下列範例會顯示為了達到所要的結果,正確撰寫查詢的方法。The following example shows the correct method of writing this query to achieve the desired result.

SELECT Model, Color, Price  
FROM (SELECT TOP(1) Model, Color, Price  
      FROM dbo.Cars  
      WHERE Color = 'red'  
      ORDER BY Price ASC) AS a  
UNION ALL  
SELECT Model, Color, Price  
FROM (SELECT TOP(1) Model, Color, Price  
      FROM dbo.Cars  
      WHERE Color = 'blue'  
      ORDER BY Price ASC) AS b;  
GO    

藉由在子選擇作業中使用 TOP 和 ORDER BY,可以確保 ORDER BY 子句的結果會套用到 TOP 子句,而不會排序 UNION 作業的結果。By using TOP and ORDER BY in a subselect operation, you ensure that the results of the ORDER BY clause are applied to the TOP clause and not to sorting the result of the UNION operation.

以下為結果集。Here is the result set.

Model         Color      Price  
------------- ---------- -------  
sedan         red        10000.00  
van           blue        8000.00

限制事項Limitations and Restrictions

當您搭配 INSERT、UPDATE、MERGE 或 DELETE 使用 TOP 時,不會以任何順序排列參考的資料列。When you use TOP with INSERT, UPDATE, MERGE, or DELETE, the referenced rows aren't arranged in any order. 而且,您無法在這些陳述式中直接指定 ORDER BY 子句。And, you can't directly specify the ORDER BY clause in these statements. 如果您需要使用 TOP 依有意義的時序來插入、刪除或修改資料列,請將 TOP 與子選擇陳述式中指定的 ORDER BY 子句搭配使用。If you need to use TOP to insert, delete, or modify rows in a meaningful chronological order, use TOP with an ORDER BY clause specified in a subselect statement. 請參閱本文稍後的<範例>一節。See the following Examples section in this article.

您不能在分割區檢視上的 UPDATE 和 DELETE 陳述式中使用 TOP。You can't use TOP in an UPDATE and DELETE statements on partitioned views.

您不能將 TOP 與相同查詢運算式 (相同查詢範圍) 中的 OFFSET 和 FETCH 結合。You can't combine TOP with OFFSET and FETCH in the same query expression (in the same query scope). 如需詳細資訊,請參閱 ORDER BY 子句 (Transact-SQL)For more information, see ORDER BY Clause (Transact-SQL).

範例Examples

類別Category 代表性語法元素Featured syntax elements
基本語法Basic syntax TOP • PERCENTTOP • PERCENT
包括相同值Including tie values WITH TIESWITH TIES
限制受 DELETE、INSERT 或 UPDATE 影響的資料列Limiting the rows affected by DELETE, INSERT, or UPDATE DELETE • INSERT • UPDATEDELETE • INSERT • UPDATE

基本語法Basic syntax

本節的範例會使用所需的最少語法來示範 ORDER BY 子句的基本功能。Examples in this section demonstrate the basic functionality of the ORDER BY clause using the minimum required syntax.

A.A. 搭配常數值使用 TOPUsing TOP with a constant value

下列範例使用常數值,來指定查詢結果集內傳回的員工人數。The following examples use a constant value to specify the number of employees that are returned in the query result set. 在第一個範例中,因為未使用 ORDER BY 子句,所以會傳回前 10 個未定義的資料列。In the first example, the first 10 undefined rows are returned because an ORDER BY clause isn't used. 在第二個範例中,ORDER BY 子句用來傳回前 10 名最近雇用的員工。In the second example, an ORDER BY clause is used to return the top 10 recently hired employees.

USE AdventureWorks2012;  
GO  
-- Select the first 10 random employees.  
SELECT TOP(10)JobTitle, HireDate  
FROM HumanResources.Employee;  
GO  
-- Select the first 10 employees hired most recently.  
SELECT TOP(10)JobTitle, HireDate  
FROM HumanResources.Employee  
ORDER BY HireDate DESC;  
GO  

B.B. 搭配變數使用 TOPUsing TOP with a variable

下列範例使用變數,來指定查詢結果集內傳回的員工人數。The following example uses a variable to specify the number of employees that are returned in the query result set.

USE AdventureWorks2012;  
GO  
DECLARE @p AS int = 10;  
SELECT TOP(@p)JobTitle, HireDate, VacationHours  
FROM HumanResources.Employee  
ORDER BY VacationHours DESC;  
GO  

C.C. 指定百分比Specifying a percentage

下列範例使用 PERCENT,來指定查詢結果集內傳回的員工人數。The following example uses PERCENT to specify the number of employees that are returned in the query result set. HumanResources.Employee 資料表有 290 名員工。There are 290 employees in the HumanResources.Employee table. 因為 290 的 5% 是含小數的值,所以此值會無條件進位到下一個整數。Because five percent of 290 is a fractional value, the value is rounded up to the next whole number.

USE AdventureWorks2012;  
GO  
SELECT TOP(5)PERCENT JobTitle, HireDate  
FROM HumanResources.Employee  
ORDER BY HireDate DESC;  
GO    

包括相同值Including tie values

A.A. 使用 WITH TIES 包含符合最後一個資料列中之值的資料列Using WITH TIES to include rows that match the values in the last row

下列範例會取得所有員工中薪資最高的 10%,並依照薪資的遞減順序傳回。The following example gets the top 10 percent of all employees with the highest salary and returns them in descending order according to their salary. 指定 WITH TIES 可確保任何薪資是所傳回之最低薪資 (最後一個資料列) 的員工也會包含在結果集中,即使這樣會超出員工的 10%,也是如此。Specifying WITH TIES ensures that employees with salaries equal to the lowest salary returned (the last row) are also included in the result set, even if it exceeds 10 percent of employees.

USE AdventureWorks2012;  
GO  
SELECT TOP(10) PERCENT WITH TIES  
pp.FirstName, pp.LastName, e.JobTitle, e.Gender, r.Rate  
FROM Person.Person AS pp   
    INNER JOIN HumanResources.Employee AS e  
        ON pp.BusinessEntityID = e.BusinessEntityID  
    INNER JOIN HumanResources.EmployeePayHistory AS r  
        ON r.BusinessEntityID = e.BusinessEntityID  
ORDER BY Rate DESC;  
GO    

限制受 DELETE、INSERT 或 UPDATE 影響的資料列Limiting the rows affected by DELETE, INSERT, or UPDATE

A.A. 使用 TOP 限制刪除的資料列數目Using TOP to limit the number of rows deleted

當您搭配 DELETE 使用 TOP (n) 子句時,會以未定義的方式選取 n 個資料列來執行刪除作業。When you use a TOP (n) clause with DELETE, the delete operation is done on an undefined selection of n number of rows. 也就是說,DELETE 陳述式會選擇符合 WHERE 子句中所定義準則的任意 (n) 數目資料列。That is, the DELETE statement chooses any (n) number of rows that meet the criteria defined in the WHERE clause. 下列範例會從 PurchaseOrderDetail 資料表刪除到期日早於 2002 年 7 月 1 日的 20 個資料列。The following example deletes 20 rows from the PurchaseOrderDetail table that have due dates earlier than July 1, 2002.

USE AdventureWorks2012;  
GO  
DELETE TOP (20)   
FROM Purchasing.PurchaseOrderDetail  
WHERE DueDate < '20020701';  
GO  

如果您要使用 TOP 依有意義的時序來刪除資料列,請在子選擇陳述式中搭配使用 TOP 和 ORDER BY。If you want to use TOP to delete rows in a meaningful chronological order, use TOP with ORDER BY in a subselect statement. 下列查詢會刪除 PurchaseOrderDetail 資料表中具有最早到期日的 10 個資料列。The following query deletes the 10 rows of the PurchaseOrderDetail table that have the earliest due dates. 為確保只刪除 10 個資料列,subselect 陳述式 (PurchaseOrderID) 中指定的資料行是資料表的主索引鍵。To ensure that only 10 rows are deleted, the column specified in the subselect statement (PurchaseOrderID) is the primary key of the table. 如果指定的資料行包含重複值,則在 subselect 陳述式中使用非索引鍵資料行會造成刪除 10 個以上的資料列。Using a nonkey column in the subselect statement may result in the deletion of more than 10 rows if the specified column contains duplicate values.

USE AdventureWorks2012;  
GO  
DELETE FROM Purchasing.PurchaseOrderDetail  
WHERE PurchaseOrderDetailID IN  
   (SELECT TOP 10 PurchaseOrderDetailID   
    FROM Purchasing.PurchaseOrderDetail   
    ORDER BY DueDate ASC);  
GO  

B.B. 使用 TOP 限制插入的資料列數目Using TOP to limit the number of rows inserted

下列範例會建立 EmployeeSales 資料表,且會將 HumanResources.Employee 資料表的前五名員工的姓名和今初至今的銷售資料插入其中。The following example creates the table EmployeeSales and inserts the name and year-to-date sales data for the top five employees from the table HumanResources.Employee. INSERT 陳述式會選擇 SELECT 所傳回且符合 WHERE 子句所定義準則的任五個資料列。The INSERT statement chooses any five rows returned by the SELECT statement that meet the criteria defined in the WHERE clause. OUTPUT 子句會顯示插入到 EmployeeSales 資料表的資料列。The OUTPUT clause displays the rows that are inserted into the EmployeeSales table. 請注意,SELECT 陳述式中的 ORDER BY 子句不會用來判斷前五名員工。Notice that the ORDER BY clause in the SELECT statement isn't used to determine the top five employees.

USE AdventureWorks2012 ;  
GO  
IF OBJECT_ID ('dbo.EmployeeSales', 'U') IS NOT NULL  
    DROP TABLE dbo.EmployeeSales;  
GO  
CREATE TABLE dbo.EmployeeSales  
( EmployeeID   nvarchar(11) NOT NULL,  
  LastName     nvarchar(20) NOT NULL,  
  FirstName    nvarchar(20) NOT NULL,  
  YearlySales  money NOT NULL  
 );  
GO  
INSERT TOP(5)INTO dbo.EmployeeSales  
    OUTPUT inserted.EmployeeID, inserted.FirstName, inserted.LastName, inserted.YearlySales  
    SELECT sp.BusinessEntityID, c.LastName, c.FirstName, sp.SalesYTD   
    FROM Sales.SalesPerson AS sp  
    INNER JOIN Person.Person AS c  
        ON sp.BusinessEntityID = c.BusinessEntityID  
    WHERE sp.SalesYTD > 250000.00  
    ORDER BY sp.SalesYTD DESC;  
GO    

如果您要使用 TOP 依有意義的時序來插入資料列,請在子選擇陳述式中搭配使用 TOP 和 ORDER BY。If you want to use TOP to insert rows in a meaningful chronological order, use TOP with ORDER BY in a subselect statement. 下列範例顯示如何執行這項工作。The following example show how to do this. OUTPUT 子句會顯示插入到 EmployeeSales 資料表的資料列。The OUTPUT clause displays the rows that are inserted into the EmployeeSales table. 請注意,現在插入的前五名員工是依據 ORDER BY 子句的結果,而不是未定義的資料列。Notice that the top five employees are now inserted based on the results of the ORDER BY clause instead of undefined rows.

INSERT INTO dbo.EmployeeSales  
    OUTPUT inserted.EmployeeID, inserted.FirstName, inserted.LastName, inserted.YearlySales  
    SELECT TOP (5) sp.BusinessEntityID, c.LastName, c.FirstName, sp.SalesYTD   
    FROM Sales.SalesPerson AS sp  
    INNER JOIN Person.Person AS c  
        ON sp.BusinessEntityID = c.BusinessEntityID  
    WHERE sp.SalesYTD > 250000.00  
    ORDER BY sp.SalesYTD DESC;  
GO    

C.C. 使用 TOP 限制更新的資料列數目Using TOP to limit the number of rows updated

下列範例會利用 TOP 子句來更新資料表中的資料列。The following example uses the TOP clause to update rows in a table. 當您搭配 UPDATE 使用 TOP (n) 子句時,會對未定義數目的資料列執行更新作業。When you use a TOP (n) clause with UPDATE, the update operation runs on an undefined number of rows. 也就是說,UPDATE 陳述式會選擇符合 WHERE 子句中所定義準則的任意 (n) 數目資料列。That is, the UPDATE statement chooses any (n) number of rows that meet the criteria defined in the WHERE clause. 下列範例會從某位銷售人員指派 10 位客戶給另一位銷售人員。The following example assigns 10 customers from one salesperson to another.

USE AdventureWorks2012;  
UPDATE TOP (10) Sales.Store  
SET SalesPersonID = 276  
WHERE SalesPersonID = 275;  
GO  

如果您必須使用 TOP 依有意義的時間順序套用更新,就要在子選擇陳述式中同時使用 TOP 與 ORDER BY。If you have to use TOP to apply updates in a meaningful chronology, you must use TOP together with ORDER BY in a subselect statement. 下例會更新最早雇用的前 10 名員工的休假時數。The following example updates the vacation hours of the 10 employees with the earliest hire dates.

UPDATE HumanResources.Employee  
SET VacationHours = VacationHours + 8  
FROM (SELECT TOP 10 BusinessEntityID FROM HumanResources.Employee  
     ORDER BY HireDate ASC) AS th  
WHERE HumanResources.Employee.BusinessEntityID = th.BusinessEntityID;  
GO  

範例:Azure Synapse Analytics (SQL 資料倉儲)Azure Synapse Analytics (SQL Data Warehouse)平行處理資料倉儲Parallel Data WarehouseExamples: Azure Synapse Analytics (SQL 資料倉儲)Azure Synapse Analytics (SQL Data Warehouse) and 平行處理資料倉儲Parallel Data Warehouse

下列範例會傳回符合查詢準則的前 31 個資料列。The following example returns the top 31 rows that match the query criteria. ORDER BY 子句可確保 31 個傳回的資料列是根據 LastName 資料行字母順序的前 31 個資料列。The ORDER BY clause ensures that the 31 returned rows are the first 31 rows based on an alphabetical ordering of the LastName column.

使用 TOP,而不需要指定繫結。Using TOP without specifying ties.

SELECT TOP (31) FirstName, LastName   
FROM DimEmployee ORDER BY LastName;  

結果:傳回 31 個資料列。Result: 31 rows are returned.

使用 TOP,指定 WITH TIES。Using TOP, specifying WITH TIES.

SELECT TOP (31) WITH TIES FirstName, LastName   
FROM DimEmployee ORDER BY LastName;  

結果:傳回 33 個資料列,因為名為 Brown 的三位員工都繫結到第 31 個資料列。Result: 33 rows are returned, because three employees named Brown tie for the 31st row.

另請參閱See Also

SELECT (Transact-SQL) SELECT (Transact-SQL)
INSERT (Transact-SQL) INSERT (Transact-SQL)
UPDATE (Transact-SQL) UPDATE (Transact-SQL)
DELETE (Transact-SQL) DELETE (Transact-SQL)
ORDER BY 子句 (Transact-SQL) ORDER BY Clause (Transact-SQL)
SET ROWCOUNT (Transact-SQL) SET ROWCOUNT (Transact-SQL)
MERGE (Transact-SQL)MERGE (Transact-SQL)