代码如下:
Create table #tt( id varchar(8),name varchar(8))
Insert into #tt
select '01','a'union all select '01','b'
union all select '01','a'
union all select '01','a'
union all select '01','b'
declare @ss varchar(8),
@col varchar(8)
Select @ss='+', @col=''Update #tt set name=@ss,@ss=(Case when (@col<>'') and (@col<>name)
then (Case when @ss='-' then '+' else '-' end) else @ss end),@col=nameSelect * from #ttdrop table #tt那个update 是怎么执行的?我怎么也看不明白!
请高手解惑!
Create table #tt( id varchar(8),name varchar(8))
Insert into #tt
select '01','a'union all select '01','b'
union all select '01','a'
union all select '01','a'
union all select '01','b'
declare @ss varchar(8),
@col varchar(8)
Select @ss='+', @col=''Update #tt set name=@ss,@ss=(Case when (@col<>'') and (@col<>name)
then (Case when @ss='-' then '+' else '-' end) else @ss end),@col=nameSelect * from #ttdrop table #tt那个update 是怎么执行的?我怎么也看不明白!
请高手解惑!
then (Case when @ss='-' then '+' else '-' end) else @ss end),@col=name在执行update的时候,对于name这样的数据库字段永远都是取update执行开始之前的数据库值,但是对于@ss这样的变量则读取变量的最新值。因此,对于“(@col<>name) ”、“@col=name”这个地方要注意name值不受“name=@ss”的影响,永远是正在计算的那一行的原来数据库的值。但是变量@ss却可以把上一行计算出来的值带给下一行参与计算。
delete 和 insert 两个步骤来执行
在update还没有完成
name的值为deleted里面的值 , 也就是原来的值
不是新的值
——————————————————————————————————————————————
绝对不要这样去偷换实际的数据库系统操作,否则很多地方会错误地“以为”逻辑结果该怎样怎样。update、insert、delete是独立的原子操作,执行机制并不重合。
delete 和 insert 两个步骤来执行
在update还没有完成
name的值为deleted里面的值 , 也就是原来的值
不是新的值
--------------------------
我一直也是这样认为的,难道不对吗?
我觉得系统在底层处理理,应该是先删了,再添加的,不然的话,底层如何操作呢?