title: How to avoid duplicated FETCH in T-SQL when using a cursor?
body: <p>In T-SQL, when iterating results from a cursor, it seems to be common practice to repeat the <code>FETCH</code> statement before the <code>WHILE</code> loop. The below example from <a href="http://msdn.microsoft.com/en-us/library/ms178642.aspx" rel="noreferrer">Microsoft</a>:</p>
  
  <pre><code>DECLARE Employee_Cursor CURSOR FOR
  SELECT EmployeeID, Title FROM AdventureWorks2012.HumanResources.Employee
      WHERE JobTitle = 'Marketing Specialist';
  OPEN Employee_Cursor;
  
  FETCH NEXT FROM Employee_Cursor;
  WHILE @@FETCH_STATUS = 0
      BEGIN
          FETCH NEXT FROM Employee_Cursor;
      END;
  CLOSE Employee_Cursor;
  DEALLOCATE Employee_Cursor;
  GO
  </code></pre>
  
  <p>(Notice how <code>FETCH NEXT FROM Employee_Cursor;</code> appears twice.)</p>
  
  <p>If the <code>FETCH</code> selects into a long list of variables, then we have a large duplicated statement which is both ugly and of course, "non-DRY" code.</p>
  
  <p>I'm not aware of a post-condition control-of-flow T-SQL statement so it seems I'd have to resort to a <code>WHILE(TRUE)</code> and then <code>BREAK</code> when <code>@@FETCH_STATUS</code> is not zero. This feels clunky to me.</p>
  
  <p>What other options do I have?</p>