广怀 数据库查询相关
本帖最后由 李维强-15级 于 2022-8-19 16:32 编辑递归查询部门名称
BEGIN
WITH CTE AS(
SELECT ID,depNum,depName,depOwnerName,depName as UpDepName FROM t_b_Department WHERE ParentDepId=0
UNION ALL
SELECT b.ID,b.depNum,b.depName,b.depOwnerName,p.depName as UpDepName FROM CTE p INNER JOIN t_b_Department b ON p.ID=b.ParentDepId
)
SELECT depNum,depName,depOwnerName,UpDepName FROM CTE
END
WITH CTE AS
(
SELECT ID,depNum,depName,depOwnerName,depName as UpDepName FROM t_b_Department WHERE ParentDepId=79 AND ID=70
UNION ALL
SELECT b.ID,b.depNum,b.depName,b.depOwnerName,p.depName as UpDepName FROM CTE p INNER JOIN t_b_Department b ON p.ID=b.ParentDepId
)
SELECT ID,depNum,depName,depOwnerName,UpDepName FROM CTE
页:
[1]