• Carlo Romagnano (12/30/2010)


    Try this for fun:

    DECLARE @MyTable TABLE (Col1 INT,

    Col2 INT

    ,Col3 INT)

    DECLARE @i INT

    -- Col1 Col2

    INSERT INTO @MyTable(Col1,Col2) VALUES (1, 2)

    INSERT INTO @MyTable(Col1,Col2) VALUES (3, 4)

    SET @i = 0

    UPDATE @MyTable SET

    @i = @i + Col1

    , Col1 = Col2

    , Col2 = Col1

    , Col3 = @i

    SELECT Col1, Col2, Col3 FROM @MyTable

    Mixing column swap with quirky update in this form is perhaps interesting, but certainly not deterministic - even though there are no indexes involved so that the only thing likely to disturb the sequencing is parallelism and doing this serially is probably going to be faster than the synchronisation overhead incurred in doing it in parallel so the optimiser is unlikely to choose parallel evaluation, there's nothing in the language specification that guarantees the order in which the rows are processed so the system can choose either row to process first or indeed can process both rows at once, which means there are 3 different results all of which would be "correct". So if you were relying on what goes into col3, you would need to add protective code to check that the (inter-row) sequencing is what you expected and abort the operation if it isn't.

    Tom