WITH dc (UserID, UserName, NickName, ParentAgencyID,AgencyName)
AS
(
-- Anchor member definition,最顶级,我们用0级来表示
SELECT e.UserID, e.UserName, e.NickName, e.ParentAgencyID,e.AgencyName--,0 AS Level
FROM Web_vUserList AS e
where e.UserID =267898
UNION ALL
-- Recursive member definition,其他各级,我们用1,2,3……级来表示
SELECT e.UserID, e.UserName, e.NickName, e.ParentAgencyID,e.AgencyName--,Level + 1
FROM dc AS d
INNER JOIN Web_vUserList AS e ON e.UserID = d.ParentAgencyID
--where (e.UserID=467083 )
)
-- Recursive member definition 递归查询的Sql,还可以嵌套使用update语句哟!
SELECT * FROM dc AS dp
下线已经确定了,现在我想确定上线。应该怎样去做呢?
------解决方案--------------------
你的上线是不是指上层级的数据
你以上不就是取的上层级的数据吗?
取下级把
INNER JOIN Web_vUserList AS e ON e.UserID = d.ParentAgencyID
改为 :
INNER JOIN Web_vUserList AS e ON d.UserID = e.ParentAgencyID
------解决方案--------------------
WITH dc (UserID, UserName, NickName, ParentAgencyID,AgencyName)
AS
(
SELECT e.UserID, e.UserName, e.NickName, e.ParentAgencyID,e.AgencyName--,0 AS Level
FROM Web_vUserList AS e
where e.UserID =267898
UNION ALL
SELECT e.UserID, e.UserName, e.NickName, e.ParentAgencyID,e.AgencyName--,Level + 1
FROM dc AS d
INNER JOIN Web_vUserList AS e ON e.ParentAgencyID = d.UserID
)
SELECT * FROM dc AS dp
------解决方案--------------------
大约类似于这样试试
WITH dc1 (UserID, UserName, NickName, ParentAgencyID,AgencyName)
AS
(
-- Anchor member definition,最顶级,我们用0级来表示
SELECT e.UserID, e.UserName, e.NickName, e.ParentAgencyID,e.AgencyName--,0 AS Level
FROM Web_vUserList AS e
where e.UserID =267898
UNION ALL
-- Recursive member definition,其他各级,我们用1,2,3……级来表示
SELECT e.UserID, e.UserName, e.NickName, e.ParentAgencyID,e.AgencyName--,Level + 1
FROM dc AS d
INNER JOIN Web_vUserList AS e ON e.UserID = d.ParentAgencyID
--where (e.UserID=467083 )
),
dc2 (UserID, UserName, NickName, ParentAgencyID,AgencyName)
AS
(
-- Anchor member definition,最顶级,我们用0级来表示
SELECT e.UserID, e.UserName, e.NickName, e.ParentAgencyID,e.AgencyName--,0 AS Level
FROM Web_vUserList AS e
where e.UserID =267898
UNION ALL
-- Recursive member definition,其他各级,我们用1,2,3……级来表示
SELECT e.UserID, e.UserName, e.NickName, e.ParentAgencyID,e.AgencyName--,Level + 1
FROM dc AS d
INNER JOIN Web_vUserList AS e ON d.UserID = e.ParentAgencyID
--where (e.UserID=467083 )
)
SELECT * FROM dc1
union all
SELECT * FROM dc2
order by UserID, ParentAgencyID
------解决方案--------------------