title: SQL Server batch delete using WHILE loop does not work
body: <p>I have a very large table, so I am using the following to delete older entries:</p>
  
  <pre><code>WHILE (@@ROWCOUNT &gt; 0)
  BEGIN
      DELETE TOP (5000) FROM myTable
      WHERE date &lt; 20130103
  END
  </code></pre>
  
  <p>I have run this a few times using different dates.  Sometimes it works fine (takes about 20 minutes), but other times the query finishes immediately and nothing has been deleted.  When that happens, I just do a simple SELECT statement from that table, and then try the above WHILE statement again, and then it works!  Anyone know why this is?  I need to automate this query to run on a regular basis to control the table size, but I want to make sure it actually deletes properly when it runs.  Thank you.</p>