共用方式為


撰寫巢狀 Entity SQL 查詢 (Entity SQL)

實體 SQL 是一個豐富的功能性語言。實體 SQL 的建置組塊為運算式。實體 SQL 不會限制為表格式結果集,與傳統 SQL 不同:實體 SQL 支援撰寫具有常值、參數或巢狀運算式的複雜運算式。運算式中的值可以參數化,或是由某個其他運算式所組成。

巢狀運算式

巢狀運算式可以放在它傳回之型別的值接受的任何地方。例如:

-- Returns a hierarchical collection of three elements at top-level. 
-- x must be passed in the parameter collection.
ROW(@x, {@x}, {@x, 4, 5}, {@x, 7, 8, 9})

-- Returns a hierarchical collection of one element at top-level.
-- x must be passed in the parameter collection.
{{{@x}}};

巢狀查詢可放在投影子句中。例如:

-- Returns a collection of rows where each row contains an Address entity.
-- and a collection of references to its corresponding SalesOrderHeader entities.
SELECT address, (SELECT DEREF(soh) 
                    FROM NAVIGATE(address, AdventureWorksModel.FK_SalesOrderHeader_Address_BillToAddressID) AS soh) 
                    AS salesOrderHeader FROM AdventureWorksEntities.Address AS address

在 實體 SQL 中,巢狀查詢一定要括在括號中:

-- Pseudo-Entity SQL
( SELECT …
FROM … )
UNION ALL
( SELECT …
FROM … );

下列範例示範如何適當地將 實體 SQL 中的運算式巢狀化:HOW TO:排序兩個聯集的查詢 (Entity Framework)

投影中的巢狀查詢

投影子句中的巢狀查詢可能會在伺服器上轉譯成笛卡兒乘積 (Cartesian Product) 查詢。在包括 SQL Server 的某些後端伺服器上,這樣可能會導致 TempDB 資料表變得相當大,因而對伺服器效能造成不良影響。

下列是這類查詢的範例:

SELECT c, (SELECT c, (SELECT c FROM AdventureWorksModel.Vendor AS c  ) As Inner2 FROM AdventureWorksModel.JobCandidate AS c  ) As Inner1 FROM AdventureWorksModel.EmployeeDepartmentHistory AS c

排序巢狀查詢

在 Entity Framework 中,巢狀運算式可放在查詢中的任何地方。由於 Entity SQL 在撰寫查詢方面提供很大的彈性,所以您可以撰寫包含巢狀查詢排序的查詢。不過,系統不會保留巢狀查詢的順序。

-- The following query will order the results by last name.
SELECT C1.FirstName, C1.LastName
        FROM AdventureWorksModel.Contact as C1
        ORDER BY C1.LastName

-- In the following query, ordering of the nested query is ignored.
SELECT C2.FirstName, C2.LastName
    FROM (SELECT C1.FirstName, C1.LastName
        FROM AdventureWorksModel.Contact as C1
        ORDER BY C1.LastName) as C2

另請參閱

概念

Entity SQL 概觀