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;