Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For variable assignment, <a href="https://stackoverflow.com/a/5758032/643591">Martin Smith answers this question here</a> referencing <a href="http://msdn.microsoft.com/en-us/library/ms187953.aspx" rel="nofollow noreferrer">MSDN</a>:</p> <blockquote> <p>If there are multiple assignment clauses in a single SELECT statement, SQL Server does not guarantee the order of evaluation of the expressions. Note that effects are only visible if there are references among the assignments.</p> </blockquote> <h2>But...</h2> <p>If we're dealing with tables, instead of with variables, it is a different story.</p> <p>In this case, Sql Server uses an <em>All-At-Once</em> operation, as discussed by <a href="http://books.google.com/books?id=v_T82PlI3UwC&amp;pg=PT470&amp;dq=t-sql%20fundamentals%20all-at-once%20update&amp;hl=en&amp;sa=X&amp;ei=CFkdUZfGDOHFigLEqICgCA&amp;ved=0CDYQ6AEwAA" rel="nofollow noreferrer">Itzik Ben-Gan in T-Sql Fundamentals</a>.</p> <p>This concept states that all expressions in the same logical phase are evaluated as if the same point in time... regardless of their left-to-right position.</p> <p>So when dealing with the corresponding <code>UPDATE</code> statement:</p> <pre><code>DECLARE @Test TABLE ( i INT, j INT ) INSERT INTO @Test VALUES (0, 0) UPDATE @Test SET i = i + 10, j = i SELECT i, j FROM @Test </code></pre> <p>We get the following results:</p> <pre><code>i j ----------- ----------- 10 0 </code></pre> <p>And by using the <em>All-At-Once</em> evaluation... in Sql Server you can swap column values in a table without an intermediate variable / column.</p> <p>Most RBDMSs behave this way <a href="https://stackoverflow.com/a/10693062/643591">as far as I know, but MySql is an exception</a>.</p> <hr> <h2>EDIT:</h2> <blockquote> <p>Note that effects are only visible if there are references among the assignments.</p> </blockquote> <p>I understand this to mean that if you have a <code>SELECT</code> statement such as the following:</p> <pre><code>SELECT @A = ColumnA, @B = ColumnB, @C = ColumnC FROM MyTable </code></pre> <p>Then it doesn't matter what order the assignments are performed in... you'll get the same results no matter what. But if you have something like...</p> <pre><code>SELECT @A = ColumnA, @B = @A, @C = ColumnC FROM MyTable </code></pre> <p>There is now a <strong>reference among the assignments</strong> (<code>@B = @A</code>), and the order that <code>@A</code> and <code>@B</code> are assigned now matters.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload