向SQL Union All添加其他联接

时间:2018-08-28 06:14:21

标签: sql-server tsql join stored-procedures union-all

我当前的SQL union all语句引用了一个名为Person的表

ALTER PROCEDURE [dbo].[uspGetOrgChart]
@ContactID varchar(100)='NOE1WWD'
AS
BEGIN


--grab id of @contactid
DECLARE @Test varchar(36)
SELECT @Test = (SELECT ADID FROM Person c1 WHERE c1.ADID = @ContactID)


;WITH StaffTree AS 
( 
    SELECT  
        c.ADID, 
        c.First_Name, 
        c.Last_Name,
        c.Position_ID,
        c.Role_ID,
        c.Report_To_ADID, 
        c.Report_To_ADID as Manager_ID,
        cc.First_Name AS Manager_First_Name, 
        cc.Last_Name as Manager_Last_Name, 
        cc.Position_ID as Manager_Pos_ID,
        cc.Role_ID as Manager_Role_ID,
        cc.First_name + ' ' + cc.Last_name AS [ReportsTo], 
        c.First_Name + ' ' + c.Last_Name as EmployeeName,  
        1 AS LevelOf 
        FROM Person                  c 
            LEFT OUTER JOIN Person  cc ON c.Report_To_ADID = cc.ADID 
        WHERE c.ADID=@Test OR (@Test IS NULL AND c.Report_To_ADID IS NULL)

    UNION ALL 
        SELECT  
        s.ADID, 
        s.First_Name, 
        s.Last_Name, 
        s.Position_ID,
        s.Role_ID,
        s.Report_To_ADID, 
        t.ADID, 
        t.First_Name, 
        t.Last_Name, 
        t.Position_ID,
        t.Role_ID,
        t.First_Name + ' ' + t.Last_Name, 
        s.First_Name + ' ' + s.Last_Name,
        t.LevelOf+1 
        FROM StaffTree            t 
            INNER JOIN Person  s ON t.ADID=s.Report_To_ADID 
    WHERE s.Report_To_ADID=@Test OR @Test IS NULL OR t.LevelOf>1 
)
SELECT * FROM StaffTree 

END

我还有另外两个表,称为位置和角色

Position_ID Position_Detail

角色ID 角色详细信息

如何向此语句添加联接以返回Position_Detail和Role_Detail

1 个答案:

答案 0 :(得分:0)

只需在子查询中使用简单联接

ALTER PROCEDURE [dbo].[uspGetOrgChart]
@ContactID varchar(100)='NOE1WWD'
AS
BEGIN


--grab id of @contactid
DECLARE @Test varchar(36)
SELECT @Test = (SELECT ADID FROM Person c1 WHERE c1.ADID = @ContactID)

;WITH StaffTree AS 
( 
    SELECT  
        c.ADID, 
        c.First_Name, 
        c.Last_Name,
        c.Position_ID,
        c.Role_ID,
        c.Report_To_ADID, 
        c.Report_To_ADID as Manager_ID,
        cc.First_Name AS Manager_First_Name, 
        cc.Last_Name as Manager_Last_Name, 
        cc.Position_ID as Manager_Pos_ID,
        cc.Role_ID as Manager_Role_ID,
        cc.First_name + ' ' + cc.Last_name AS [ReportsTo], 
        c.First_Name + ' ' + c.Last_Name as EmployeeName,  
        1 AS LevelOf 
        FROM Person                  c 
            LEFT OUTER JOIN Person  cc ON c.Report_To_ADID = cc.ADID 
        WHERE c.ADID=@Test OR (@Test IS NULL AND c.Report_To_ADID IS NULL)

    UNION ALL 
        SELECT  
        s.ADID, 
        s.First_Name, 
        s.Last_Name, 
        s.Position_ID,
        s.Role_ID,
        s.Report_To_ADID, 
        t.ADID, 
        t.First_Name, 
        t.Last_Name, 
        t.Position_ID,
        t.Role_ID,
        t.First_Name + ' ' + t.Last_Name, 
        s.First_Name + ' ' + s.Last_Name,
        t.LevelOf+1 
        FROM StaffTree            t 
            INNER JOIN Person  s ON t.ADID=s.Report_To_ADID 
    WHERE s.Report_To_ADID=@Test OR @Test IS NULL OR t.LevelOf>1 
)
SELECT s.*,p.Position_Detail,r.Role_Detail FROM StaffTree s inner join Position p on s.Position_ID=p.Position_ID
inner join Role r on s.Role_ID=r.Role_ID
  END