중첩 Entity SQL 쿼리 작성(Entity SQL)
Entity SQL 은 다양한 기능을 가진 언어입니다. Entity SQL 의 빌딩 블록은 식입니다. Entity SQL 은 기존 SQL과 달리 테이블 형식의 결과 집합으로 한정되지 않습니다. Entity 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
Entity SQL 에서 중첩 쿼리는 반드시 괄호로 묶어야 합니다.
-- Pseudo-Entity SQL
( SELECT …
FROM … )
UNION ALL
( SELECT …
FROM … );
다음 예제에서는 Entity SQL 에서 식을 올바르게 중첩하는 방법을 보여 줍니다. 방법: 두 쿼리의 통합 정렬(Entity Framework)을 참조하십시오.
프로젝션의 중첩 쿼리
프로젝션 절의 중첩 쿼리는 서버의 Cartesian 제품 쿼리로 변환될 수 있습니다. SLQ 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