执行顺序:从左到右,变量优先,逐行更新
摘自CSDN的例子(http://topic.csdn.net/u/20091030/16/7fd75fa6-bdb9-4516-9b27-48aef69703ba.html
http://topic.csdn.net/u/20090904/16/e5dad9c7-fb59-41b9-b28d-e3b71c3e8420.html)
1.变量优先
create table #t (field1 varchar(10),field2 varchar(10))
insert #t select 'a','b'
declare @str varchar(10)
set @str='Test'
update #t set Field1=@str,@str=Field2,Field2=Field1
select * from #t
drop table #t
/*field1 field2
---------- ----------
b a*/
2.逐步更新
if OBJECT_ID('ta') is not null drop table ta
create table ta(empNo varchar(8) ,empName varchar(20),nIndex int) --drop table ta
insert into ta
select 'A0001','張三',null
union
select 'A0002','李四',null
union
select 'A0003','王二',null
union
select 'A0004','趑六',null
declare @tmpIndex int
set @tmpIndex=0
update ta set nIndex=@tmpIndex,@tmpIndex=@tmpIndex+1
select * from ta
/*
empNo empName nIndex
A0001 張三 1
A0002 李四 2
A0003 王二 3
A0004 趑六 4
*/
(1 行受影响)*/
3.字段之间, 并行执行
create table #t (field1 varchar(10),field2 varchar(10))
insert #t select 'a','b'
declare @str varchar(10)
update #t set Field1=Field2,Field2=Field1
select * from #t
/*
field1 field2
---------- ----------
b a
*/
1, 先变量再字段
2, 变量之间, 从左到右
3, 字段之间, 并行执行