SQL Update語句的執行順序

2021-05-09 20:55:49 字數 2063 閱讀 9059

執行順序:從左到右,變數優先,逐行更新

摘自csdn的例子(http://topic.csdn.net/u/20091030/16/7fd75fa6-bdb9-4516-9b27-48aef69703ba.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

#tdrop

table

#t/*

field1     field2

---------- ----------

b          a*/

2.逐步更新 if

object_id('

ta') is

notnull

drop

table

tacreate

table

ta(empno

varchar(8

) ,empname

varchar(20

),nindex

int) 

--drop table ta

insert

into

taselect

'a0001',

'張三',

null

union

select

'a0002',

'李四',

null

union

select

'a0003',

'王二',

null

union

select

'a0004',

'趑六',

null

declare

@tmpindex

intset

@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, 字段之間, 並行執行

SQL update語句加減乘除運

在處理提現操作時,遇到這樣乙個問題 account money為使用者剩餘金額,提現之後要減去相應金額,需要用update語句進行運算,記錄一下 update之前資料庫資料 下邊所有的資料演示都是與前一步資料進行對比 1 減法 update money user bank set account m...

SQL update語句加減乘除運算

在處理提現操作時,遇到這樣乙個問題 account money為使用者剩餘金額,提現之後要減去相應金額,需要用update語句進行運算,記錄一下 update之前資料庫資料 下邊所有的資料演示都是與前一步資料進行對比 1 減法 update money user bank set account m...

靜態語句塊 構造語句塊以及建構函式的執行順序

class helloa static public class hellob extends helloa static public static void main string args 輸出 static astatic b main start i m a class helloa i ...