title: Can I do without this cursor
body: <p>I have a junction table which holds dependencies between items.  I am using this to programmatically create a gantt chart showing these dependencies.  I have a working stored procedure now, however my company's policy is to avoid cursors where possible.  I put this to the gurus out there, is it possible to do this without the cursor? 
  DATA:
  <code><pre>
  declare @BaseTable Table
  (
      [IssueDependencyId] [bigint] IDENTITY(1,1) NOT NULL,
      [IssueId] [bigint] NOT NULL,
      [DependsOnIssueId] [bigint] NOT NULL
  )</p>
  
  <p>INSERT INTO @BaseTable
  SELECT 
  48, 0
  UNION ALL SELECT
  49, 48
  UNION ALL SELECT
  50, 48
  UNION ALL SELECT
  51, 48
  UNION ALL SELECT
  55, 48
  UNION ALL SELECT
  56, 48
  UNION ALL SELECT
  52, 49
  UNION ALL SELECT
  52, 50
  UNION ALL SELECT
  52, 51
  UNION ALL SELECT
  53, 52
  UNION ALL SELECT
  57, 54
  UNION ALL SELECT
  54, 55
  UNION ALL SELECT
  57, 56</p>
  
  <p>SELECT * FROM @BaseTable
  </pre></code></p>
  
  <p>STORED PROC code:</p>
  
  <pre><code>DECLARE @IssueId int, @DependsOnIssueId int, @StartPoint int, @EndPoint int
  SET @StartPoint = 0
  SET @EndPoint = 10
  
  DECLARE @ResultsTable TABLE (
  IssueId int not null,
  DependsOnIssueId int not null,
  Start_Point int,
  End_Point int
  )
  Select IssueId, DependsOnIssueId 
   INTO #tmp1 
   FROM IssueDependency 
   WHERE UpperLevelIssueId = 48 
   ORDER BY DependsOnIssueId
  
   declare MyCursor Cursor for (Select IssueId, DependsOnIssueId from #tmp1);
  
   OPEN MyCursor
  
   FETCH NEXT FROM MyCursor 
   INTO @IssueId, @DependsOnIssueId
  
   WHILE @@FETCH_STATUS = 0
   BEGIN
      --get parent position to set start
      SELECT @StartPoint = ISNULL(End_Point, 0) 
             FROM @ResultsTable WHERE IssueId = @DependsOnIssueId
      SET @EndPoint = @StartPoint + 10
  
      INSERT INTO @ResultsTable VALUES 
              (@IssueId, @DependsOnIssueId, @StartPoint, @EndPoint)
  
      FETCH NEXT FROM MyCursor 
      INTO @IssueId, @DependsOnIssueId
   END
  
   Close MyCursor
  
   DEALLOCATE MyCursor;
  
   SELECT IssueId, 
        MAX(start_point) max_start_point, 
        MAX(end_point) max_end_point
   INTO #MaxPoints
   from @ResultsTable
   GROUP BY IssueId
  
   SELECT r.IssueId,DependsOnIssueId,
         max_start_point start_point,
          max_end_point end_point  
   FROM @ResultsTable  r
   JOIN #MaxPoints m ON m.IssueId = r.IssueId
   ORDER BY r.IssueId
  </code></pre>
  
  <p>RESULTING DATA</p>
  
  <pre><code>IssueId DependsOnIssueId    Start_Point End_Point
  --------------------------------------------------------------------
    48           0                0              10
    49           48               10             20
    50           48               10         20
    51           48               10         20
    52           49               20         30
    52           50               20         30
    52           51               20         30
    53           52               30         40
    54           55               20         30
    55           48               10         20
    56           48               10         20
    57           54               30         40
    57           56               30         40
  </code></pre>
  
  <p>Your help much appreciated!! </p>