title: How to write this recursive query correctly in the SQL Server?
body: <p>I write a recursive query. Problem: a recursive query that show chain management employees that leads to a particular employee('Maria Cameron' with <code>empid</code>=8).Output should be like this:<a href="https://i.stack.imgur.com/ipHJP.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ipHJP.png" alt="output"></a></p>
  
  <p>and <code>HR.Employees</code> is here:
  <a href="https://i.stack.imgur.com/DyPio.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DyPio.png" alt="table"></a></p>
  
  <p>and my query is here: </p>
  
  <pre><code>with Managers as
  (
  
  SELECT empid, mgrid, firstname,lastname
  FROM HR.Employees as h
  where mgrid IS NULL
  UNION ALL
  SELECT e.empid,e.mgrid,e.firstname,e.lastname
  FROM HR.Employees as e INNER JOIN Managers m 
  ON (e.mgrid = m.empid)
  )
   SELECT *
   FROM Managers
   where firstname='Maria' and lastname='Cameron' and empid=8
  </code></pre>
  
  <p>but this query don't operate correctly and my output is:
  <a href="https://i.stack.imgur.com/TVHdd.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/TVHdd.png" alt="enter image description here"></a></p>