SQL迭代查询 PL/SQL
with ORG_Tree(ObjectId,parentID)
as
(
select a.ObjectId,a.parentID from Ot_Organizationunit a where Name in ('江苏区域','浙江区域','苏州区域')
Union ALL
select b.ObjectId,b.parentID from Ot_Organizationunit b inner join ORG_Tree T on b.parentID=T.ObjectID
)
select ouser.Name,ouser.code from OT_User ouser inner join ORG_Tree ot on ouser.parentid=ot.ObjectID
-- Get childs by parent id
WITH Tree
AS
(
SELECT Id,ParentId FROM dbo.Node P WHERE P.Id = 21 -- parent id
UNION ALL
SELECT C.Id,C.ParentId FROM dbo.Node C
INNER JOIN Tree T ON C.ParentId = T.Id
)
SELECT * FROM Tree -- Get parents by child id
WITH Tree
AS
(
SELECT Id,ParentId FROM dbo.Node C WHERE C.Id = 57 -- child id
UNION ALL
SELECT P.Id,P.ParentId FROM dbo.Node P
INNER JOIN Tree T ON P.Id = T.ParentId
)
SELECT * FROM Tree