title: SQL Server 2005: Update rows in a specified order (like ORDER BY)?
body: <p>I want to update rows of a table in a specific order, like one would expect if including an ORDER BY clause, but SQL Server does not support the ORDER BY clause in UPDATE queries.</p>
  
  <p>I have checked out <a href="https://stackoverflow.com/questions/655010/how-to-update-and-order-by-using-ms-sql">this question</a> which supplied a nice solution, but my query is a bit more complicated than the one specified there.</p>
  
  <pre><code>UPDATE TableA AS Parent
  SET Parent.ColA = Parent.ColA + (SELECT TOP 1 Child.ColA
      FROM TableA AS Child
      WHERE Child.ParentColB = Parent.ColB
      ORDER BY Child.Priority)
  ORDER BY Parent.Depth DESC;
  </code></pre>
  
  <p>So, what I'm hoping that you'll notice is that a single table (<code>TableA</code>) contains a hierarchy of rows, wherein one row can be the parent or child of any other row.  The rows need to be updated in order from the deepest child up to the root parent.  This is because <code>TableA.ColA</code> must contain an up-to-date concatenation of its own current value with the values of its children (I realize this query only concats with one child, but that is for the sake of simplicity - the purpose of the example in this question does not necessitate any more verbosity), therefore the query must update from the bottom up.</p>
  
  <p>The solution suggested in the question I noted above is as follows:</p>
  
  <pre><code>UPDATE messages
  SET status=10
  WHERE ID in (SELECT TOP (10) Id
      FROM Table
      WHERE status=0
      ORDER BY priority DESC
  );
  </code></pre>
  
  <p>The reason that I don't think I can use this solution is because I am referencing column values from the parent table inside my subquery (see <code>WHERE Child.ParentColB = Parent.ColB</code>), and I don't think two sibling subqueries would have access to each others' data.</p>
  
  <p>So far I have only determined one way to merge that suggested solution with my current problem, and I don't <em>think</em> it works.</p>
  
  <pre><code>UPDATE TableA AS Parent
  SET Parent.ColA = Parent.ColA + (SELECT TOP 1 Child.ColA
      FROM TableA AS Child
      WHERE Child.ParentColB = Parent.ColB
      ORDER BY Child.Priority)
  WHERE Parent.Id IN (SELECT Id
      FROM TableA
      ORDER BY Parent.Depth DESC);
  </code></pre>
  
  <p>The <code>WHERE..IN</code> subquery will not actually return a <em>subset</em> of the rows, it will just return the full list of IDs in the order that I want.  However (I don't know for sure - please tell me if I'm wrong) I think that the <code>WHERE..IN</code> clause will not care about the order of IDs within the parentheses - it will just check the ID of the row it currently wants to update to see if it's in that list (which, they all are) in whatever order it is already trying to update... Which would just be a total waste of cycles, because it wouldn't change anything.</p>
  
  <p>So, in conclusion, I have looked around and can't seem to figure out a way to update in a specified order (and included the <em>reason</em> I need to update in that order, because I am sure I would otherwise get the ever-so-useful "why?" answers) and I am now hitting up Stack Overflow to see if any of you gurus out there who know more about SQL than I do (which isn't saying much) know of an efficient way to do this.  It's particularly important that I only use a single query to complete this action.</p>
  
  <p>A long question, but I wanted to cover my bases and give you guys as much info to feed off of as possible. :)</p>
  
  <p>Any thoughts?</p>