Click here to Skip to main content
15,898,538 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ,

I have database , in which employee manager relationship is happening somewhere which is causing max recurssion limit

for example A->B->c->A (Employee manager relationship) now i have so many companies and so many employees in that database ..

basically i am trying for each and everycompany from below code

SQL
Declare
 @compID as int=45,
 @roleName as varchar(50) = 'manager'


Begin
WITH CTE_Emp(Id, EmpName, ManagerID, [Level], RoleName) AS
   (
    SELECT
     t1.Id,
     t1.FirstName + ' ' + t1.LastName 'EmpName',
     t1.ManagerId,
     1,
     (SELECT r1.Name FROM [Role] as r1 with (nolock) WHERE r1.Id = t1.Role_Id) 'RoleName'
    FROM Employee as t1 with (nolock)
    WHERE t1.Id  in (select e.Id from Employee as e with (nolock)) AND t1.IsDeleted = 0 AND t1.Status = 1 AND t1.Company_Id = @compID

    UNION ALL

    SELECT
     t1.Id,
     t1.FirstName + ' ' + t1.LastName 'EmpName',
     t1.ManagerId,
     (t2.[Level] + 1),
     (SELECT r1.Name FROM [Role] as r1 with (nolock) WHERE r1.Id = t1.Role_Id) 'RoleName'
    FROM Employee as t1 with (nolock)
    INNER JOIN CTE_Emp as t2 on t1.ManagerId = t2.Id
    WHERE t1.IsDeleted = 0 AND t1.[Status] = 1 AND t1.Company_Id = @compID
   )

select Id, EmpName, ManagerID, [Level], RoleName from CTE_Emp where RoleName = ISNULL(@roleName, RoleName)

End

GO




is there is any way from which i can pass all commpany id and track down on which companyId it is causing an issue .. and then i will check for perticular company employee where again is there is any way where i can send all employee to check and can be able to track down on which employee is caussing recurssion problem ??
Posted

1 solution

Something like this should work:
SQL
WITH cteData (Id, ManagerId, EmployeePath, IsLoop) As
(
    SELECT
        Id,
        ManagerId,
        '/' + CAST(Id As varchar(max)) + '/',
        CASE
            WHEN ManagerId = Id THEN CAST(1 As bit)
            ELSE CAST(0 As bit)
        END
    FROM
        Employee

    UNION ALL

    SELECT
        D.Id,
        E.ManagerId,
        D.EmployeePath + CAST(E.Id As varchar(max)) + '/',
        CASE
            WHEN D.EmployeePath Like '%' + CAST(E.Id As varchar(max)) + '%' THEN CAST(1 As bit)
            ELSE CAST(0 As bit)
        END
    FROM
        cteData As D
        INNER JOIN Employee As E
        ON E.Id = D.ManagerId
    WHERE
        D.IsLoop = 0
)
SELECT
    Id,
    EmployeePath
FROM
    cteData
WHERE
    IsLoop = 1
;

That will give you the list of all employees which are part of a loop, with the list of employee IDs that form the loop.


EDIT:
Based on the suggestion posted by Jörgen Andersson in the comments, this should give you the same results with better performance:
SQL
WITH cteData (Id, ManagerId, EmployeePath, Distance) As
(
    SELECT
        Id,
        ManagerId,
        CAST(Id As varchar(max)),
        0 As Distance
    FROM
        Employee

    UNION ALL

    SELECT
        D.Id,
        E.ManagerId,
        D.EmployeePath + ' > ' + CAST(E.Id As varchar(max)),
        D.Distance + 1
    FROM
        cteData As D
        INNER JOIN Employee As E
        ON E.Id = D.ManagerId
    WHERE
        D.ManagerId != D.Id
    And
        E.ManagerId != E.Id
)
SELECT
    Id,
    EmployeePath,
    Distance
FROM
    cteData
WHERE
    ManagerId = Id
And
    Distance != 0
;
 
Share this answer
 
v3
Comments
Maciej Los 3-Jun-15 10:51am    
Looks promisingly ;)
Jörgen Andersson 3-Jun-15 16:37pm    
Yeah that works, but performance will be a bit lacking due to the LIKE clause.
Have a look at this fiddle: http://sqlfiddle.com/#!3/14417b/4
I won't post it as a solution since I didn't create it, but it's pretty efficient.
Richard Deeming 4-Jun-15 3:26am    
Nice find! I've updated the solution with a query based on this code. :)
Torakami 3-Jun-15 22:38pm    
OK i will check this and get back to you ..thanks
Torakami 4-Jun-15 1:11am    
I didnt get what do u mean by employee path here .. there is nothing called nodeid ??

can you please eleoborate this futther

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900