The following scenario allows one Staff Manager to view all direct reports and their child member's direct reports recursively:
WITH Hierachy(StaffNo, ManagerStaffNo, PreferredName, Level)AS(SELECT StaffNo, ManagerStaffNo, PreferredName, 0 AS LevelFROM Staff sWHERE s.StaffNo = @StaffNoUNION ALLSELECT s.StaffNo, s.ManagerStaffNo, s.PreferredName, sh.Level + 1FROM Staff sINNER JOIN Hierachy sh ON s.ManagerStaffNo = sh.StaffNo)SELECT StaffNo, ManagerStaffNo, PreferredName, LevelFROM Hierachy
No comments :
Post a Comment