UPDATE Statement - How does it work internally

  • Hi,

    I have just played around with an UPDATE statement...

    CREATE TABLE updater

    (

    col1 INT,

    col2 int

    )

    DECLARE @i INT

    SET @i = 0

    WHILE @i < 100

    BEGIN

    SET @i = @i + 10

    INSERT INTO updater VALUES (@i, @i + 5)

    END

    SELECT * FROM updater

    UPDATE updater SET col2 = col1, col1 = col2

    SELECT * FROM updater

    After that the values of col1 and col2 changed places...I wonder how this works internally in SQL Server? Can anyone provide me an answer?

    Thank you!

  • It works exactly how you already have stated. I don't understand what you're missing.

    Do you mean that you would expect it to set both columns to the original col1 value?

    -- Gianluca Sartori

  • Hi,

    yes that's what I expected or thought what would happen.

    Do you know how this works internally? If it updates col2 to the value of col1 first and later col1 to the value of col2 then both cols had the value of col1 at the end...

    Thank you!

  • The UPDATE statement works internally row by row, working on the values definded beforethe update takes place.

    -- Gianluca Sartori

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply