January 12, 2010 at 3:40 am
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!
-----------------------
SQL Server Database Copy Tool at Codeplex
January 12, 2010 at 4:29 am
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
January 12, 2010 at 5:04 am
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!
-----------------------
SQL Server Database Copy Tool at Codeplex
January 12, 2010 at 6:09 am
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