title: Conditional Recursive SQL Select
body: <p>Consider the following database table. It consists of 3 columns: <em>Id</em>, <em>ParentId</em>, <em>Enabled</em>.</p>
  
  <p><a href="https://i.stack.imgur.com/71ZBM.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/71ZBM.jpg" alt="Table"></a></p>
  
  <p>I would like to produce a result set similar to the following. Basically for each record that has a <strong>Parent ID</strong>, I want to to display an additional column <strong>Enabled Parent Id</strong>. This column basically needs to recursively checks the hierarchy of the key, and stops when a key that is <strong>Enabled = True</strong> is found.</p>
  
  <p><a href="https://i.stack.imgur.com/3CVXG.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/3CVXG.jpg" alt="Result"></a></p>
  
  <p>I would like this to achieve this on the fly, without requiring to add any additional computed columns in the table.</p>
  
  <p>Maybe this could be achieved using a <strong>CTE</strong>.</p>