TSQL A recursive update?

后端 未结 1 1649
予麋鹿
予麋鹿 2020-12-19 22:15

I\'m wondering if exists a recursive update in tsql (CTE)

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


        
相关标签:
1条回答
  • 2020-12-19 22:56

    Yes, it should be. 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;
    
    0 讨论(0)
提交回复
热议问题