title: Query to flatten Employee Hierarchy
body: <p>I am just 2yrs experienced in SQL, and trying to flatten hierarchy from below table, not getting through. Appreciate your valuable suggestions.</p>
  
  <p>Requirement is to show something like <code>'bala&gt;&gt;ben&gt;&gt;mari&gt;&gt;dir&gt;&gt;cto&gt;&gt;ceo&gt;&gt;null'</code></p>
  
  <p>I tried this, but no luck.</p>
  
  <pre><code>declare @cn varchar(25), @mn varchar(25), @vr varchar(25), @tm varchar(25), @msg varchar(max);
  declare crsr cursor for
  select * from dbo.ctable ct order by ename
  open crsr
  fetch next from crsr into @cn, @mn
  set @msg = @cn
  while @@FETCH_STATUS = 0
  begin
   if @mn is not null set @msg = @msg + ' - '+@mn
   print @msg
   fetch next from crsr into @cn, @mn
  end
  close crsr
  deallocate crsr
  </code></pre>
  
  <p><img src="https://i.stack.imgur.com/IoWga.png" alt="Data table looks like this"></p>