公用表表达式(CTE)WITH:树型查询、更新
轉自http://msdn.microsoft.com/zh-cn/library/ms175972(SQL.100).aspx
?
指定臨時命名的結果集,這些結果集稱為公用表表達式 (CTE)。
語法:
[ WITH <common_table_expression> [ ,...n ] ]
<common_table_expression>::=
????????expression_name?[ ( column_name [ ,...n ] ) ]
????AS
????????( CTE_query_definition )
創建和使用公用表表達式的準則:
CTE 之后必須跟隨引用部分或全部 CTE 列的單條 SELECT、INSERT、UPDATE、MERGE 或 DELETE 語句。
?
示例:
1. 使用遞歸公用表表達式顯示遞歸的多個級別
以下示例顯示經理以及向經理報告的雇員的層次列表。?
代碼 USE AdventureWorks;GO
WITH DirectReports(ManagerID, EmployeeID, EmployeeLevel) AS
(
SELECT ManagerID, EmployeeID, 0 AS EmployeeLevel
FROM HumanResources.Employee
WHERE ManagerID IS NULL
UNION ALL
SELECT e.ManagerID, e.EmployeeID, EmployeeLevel + 1
FROM HumanResources.Employee e
INNER JOIN DirectReports d
ON e.ManagerID = d.EmployeeID
)
SELECT ManagerID, EmployeeID, EmployeeLevel
FROM DirectReports ;
GO
?
?
2. 使用遞歸公用表表達式顯示遞歸的兩個級別
以下示例顯示經理以及向經理報告的雇員。將返回的級別數目被限制為兩個。
代碼 USE AdventureWorks;GO
WITH DirectReports(ManagerID, EmployeeID, EmployeeLevel) AS
(
SELECT ManagerID, EmployeeID, 0 AS EmployeeLevel
FROM HumanResources.Employee
WHERE ManagerID IS NULL
UNION ALL
SELECT e.ManagerID, e.EmployeeID, EmployeeLevel + 1
FROM HumanResources.Employee e
INNER JOIN DirectReports d
ON e.ManagerID = d.EmployeeID
)
SELECT ManagerID, EmployeeID, EmployeeLevel
FROM DirectReports
WHERE EmployeeLevel <= 2 ;
GO
?
3. 使用遞歸公用表表達式顯示層次列表
通過縮進各個級別,突出顯示經理和雇員的層次結構。
代碼 USE AdventureWorks;GO
WITH DirectReports(Name, Title, EmployeeID, EmployeeLevel, Sort)
AS (SELECT CONVERT(varchar(255), c.FirstName + ' ' + c.LastName),
e.Title,
e.EmployeeID,
1,
CONVERT(varchar(255), c.FirstName + ' ' + c.LastName)
FROM HumanResources.Employee AS e
JOIN Person.Contact AS c ON e.ContactID = c.ContactID
WHERE e.ManagerID IS NULL
UNION ALL
SELECT CONVERT(varchar(255), REPLICATE ('| ' , EmployeeLevel) +
c.FirstName + ' ' + c.LastName),
e.Title,
e.EmployeeID,
EmployeeLevel + 1,
CONVERT (varchar(255), RTRIM(Sort) + '| ' + FirstName + ' ' +
LastName)
FROM HumanResources.Employee as e
JOIN Person.Contact AS c ON e.ContactID = c.ContactID
JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID
)
SELECT EmployeeID, Name, Title, EmployeeLevel
FROM DirectReports
ORDER BY Sort;
GO
?
4. 在 UPDATE 語句中使用遞歸 CTE
以下示例將直接或間接向 ManagerID 12 報告的所有雇員的 VacationHours 值增加 25%。公用表表達式將返回直接向 ManagerID 12
報告的雇員以及直接向這些雇員報告的雇員等的層次列表。只修改公用表表達式所返回的行。
GO
WITH DirectReports(EmployeeID, NewVacationHours, EmployeeLevel)
AS
(SELECT e.EmployeeID, e.VacationHours, 1
FROM HumanResources.Employee AS e
WHERE e.ManagerID = 12
UNION ALL
SELECT e.EmployeeID, e.VacationHours, EmployeeLevel + 1
FROM HumanResources.Employee as e
JOIN DirectReports AS d ON e.ManagerID = d.EmployeeID
)
UPDATE HumanResources.Employee
SET VacationHours = VacationHours * 1.25
FROM HumanResources.Employee AS e
JOIN DirectReports AS d ON e.EmployeeID = d.EmployeeID;
GO
?
?
轉載于:https://www.cnblogs.com/NewSunshineLife/archive/2010/07/22/1783062.html
總結
以上是生活随笔為你收集整理的公用表表达式(CTE)WITH:树型查询、更新的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: XX管理包括哪些功能
- 下一篇: [转]第(前)k大数问题