gpt4 book ai didi

TSQL 递归更新?

转载 作者:行者123 更新时间:2023-12-01 09:37:58 25 4
gpt4 key购买 nike

我想知道 tsql (CTE) 中是否存在递归更新

ID  parentID value
-- -------- -----
1 NULL 0
2 1 0
3 2 0
4 3 0
5 4 0
6 5 0

我可以使用例如 CTE 从 ID = 6 递归更新列 value 到最顶行吗?

最佳答案

是的,应该是。 MSDN gives an example :

USE AdventureWorks;
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;

关于TSQL 递归更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4324720/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com