sql - Update statements containing multiple sets -


i'm bit confused on why i'm seeing difference between 2 statements. i'm trying increment int column 1, setting values 1, 2, 3, 4, etc.:

query #1 produces desired result:

declare @a int set @a = 0 update #jc_temp  set num = @a, @a=@a+1   

query #2 sets rows 0:

declare @a int set @a = 0 update #jc_temp  set num = @a set @a=@a+1   

obviously i'm missing here, naked eye appear same. local variable rollback somehow in 2nd query?

because second "update" two separate statements:

1 - declare @a int  2 - set @a = 0  3 - update #jc_temp        set num = @a   4 - set @a=@a+1 

the update sets rows value of @a (which 0), next statement increments @a.

your first update :

1 - declare @a int 2 - set @a = 0 3 - update #jc_temp        set num = @a,           @a=@a+1  

increments value of @a for each row, see increments in data.


Comments

Popular posts from this blog

python - TypeError: start must be a integer -

c# - DevExpress RepositoryItemComboBox BackColor property ignored -

django - Creating multiple model instances in DRF3 -