如何删除树状列表中的重复项?

时间:2019-03-26 08:40:39

标签: sql sql-server list tsql tree

我是sql server中的新手,我有部门,并且用户已分配给多个部门,我需要以sp的形式获取用户部门列表,其中没有重复项。

我很累在c#中将树转换为平面列表,但是我想在不使用C#代码的情况下从sql中正确获取数据。

-- get the tree node
    SELECT     UD_nUserID, UD_nDepartmentID
FROM         UserDepartments
WHERE UD_nUserID=@UD_nUserID

UNION 

SELECT DP_nManagerID AS UD_nUserID,DP_nID AS UD_nDepartmentID
FROM dbo.Departments
WHERE DP_nManagerID=@UD_nUserID AND  DP_bIsDeleted = 0
-- then get the items per node (in two differnt sps , which is not correct at all)
SELECT
     [DP_nID]
    , [DP_cName]
    , [DP_nManagerID]
    , [DP_nParentID]
    , [DP_nBranchID]
FROM
    [dbo].[Departments]
WHERE
        [DP_nID] = @DP_nID
        AND
        [DP_bIsDeleted] = 0

我需要从此树中删除重复项:

show image  here

您能帮我写出完整的sp,返回所有没有重复项的用户部门吗?

0 个答案:

没有答案