sql 获取所有上级的实现方法
说明:
函数,直接调用即可;
数据库存在的表 ,M0OrgPID 为目标表,OrgID为ID字段,PID为上级ID字段
sql;">
DECLARE @OrgID NVARCHAR(36)= '00000000-0000-0000-00000002205223459';
--获取当前机构的所有上级 放入M0OrgPID
WITH M0OrgPID
AS ( SELECT ,0 AS lvl
FROM M0Org
WHERE OrgID = @OrgID
UNION ALL
SELECT d.,lvl + 1
FROM M0OrgPID c
INNER JOIN M0Org d ON c.PID = d.OrgID )
SELECT * FROM M0OrgPID;
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!