dbTalk Databases Forums  

Update order

comp.databases.ms-sqlserver comp.databases.ms-sqlserver


Discuss Update order in the comp.databases.ms-sqlserver forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Yannick Turgeon
 
Posts: n/a

Default Update order - 08-06-2003 , 09:06 AM






Hello,

I'm using SS2000 on NT4.

If I run the following query, is it garanteed that the field
"OldField1Value" will hold the value Field1 had before the query was run?

UPDATE MyTable
SET Field1 = 42,
OldField1Value = Field1


.... and in this query?

UPDATE MyTable
SET OldField1Value = Field1,
Field1 = 42





Reply With Quote
  #2  
Old   
Rich Dillon
 
Posts: n/a

Default Re: Update order - 08-06-2003 , 12:59 PM






Yannick,

Yes. OldField1Value will be set to the value of Field1 prior to the query's
execution in both cases. Think of all the columns in being updated in the
same instant rather than one after the other. The order in which you
specify the columns after SET is not significant.

Try this for a demonstration:

CREATE TABLE foo (
code CHAR(1) NOT NULL PRIMARY KEY,
c1 INT NOT NULL,
c2 INT NOT NULL,
c3 INT NOT NULL);

INSERT INTO foo VALUES ('A',1,3,7);
INSERT INTO foo VALUES ('B',2,5,8);

UPDATE foo
SET c1=c2+1, c2=c3+1, c3=c1+1;


"Yannick Turgeon" <nobody (AT) nowhere (DOT) com> wrote

Quote:
Hello,

I'm using SS2000 on NT4.

If I run the following query, is it garanteed that the field
"OldField1Value" will hold the value Field1 had before the query was run?

UPDATE MyTable
SET Field1 = 42,
OldField1Value = Field1


... and in this query?

UPDATE MyTable
SET OldField1Value = Field1,
Field1 = 42







Reply With Quote
  #3  
Old   
Yannick Turgeon
 
Posts: n/a

Default Re: Update order - 08-06-2003 , 01:42 PM



I just realised that your test exclude any possible "lucky" sequence. This
is a definite confirmation!

"Rich Dillon" <richdillon (AT) mindspring (DOT) com> wrote

Quote:
Yannick,

Yes. OldField1Value will be set to the value of Field1 prior to the
query's
execution in both cases. Think of all the columns in being updated in the
same instant rather than one after the other. The order in which you
specify the columns after SET is not significant.

Try this for a demonstration:

CREATE TABLE foo (
code CHAR(1) NOT NULL PRIMARY KEY,
c1 INT NOT NULL,
c2 INT NOT NULL,
c3 INT NOT NULL);

INSERT INTO foo VALUES ('A',1,3,7);
INSERT INTO foo VALUES ('B',2,5,8);

UPDATE foo
SET c1=c2+1, c2=c3+1, c3=c1+1;


"Yannick Turgeon" <nobody (AT) nowhere (DOT) com> wrote in message
news:xW7Ya.2778$_a4.574340 (AT) news20 (DOT) bellglobal.com...
Hello,

I'm using SS2000 on NT4.

If I run the following query, is it garanteed that the field
"OldField1Value" will hold the value Field1 had before the query was
run?

UPDATE MyTable
SET Field1 = 42,
OldField1Value = Field1


... and in this query?

UPDATE MyTable
SET OldField1Value = Field1,
Field1 = 42









Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.