엔터티 SQL은 다양한 기능 언어입니다. Entity SQL의 구성 요소는 식입니다. 기존 SQL과 달리 Entity 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에서 식을 올바르게 중첩하는 방법을 보여 줍니다. 방법: 두 쿼리의 합집합 순서를 지정합니다.
프로젝션의 중첩된 쿼리
프로젝트 절의 중첩된 쿼리는 서버의 Cartesian 제품 쿼리로 변환될 수 있습니다. 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