Binary Search Query using SQL
Just change this line in each CTE:
from Table1 where ParentUserID is null
to:
from Table1 where ParentUserID = @ParentId
Just change this line in each CTE:
from Table1 where ParentUserID is null
to:
from Table1 where ParentUserID = @ParentId