一条update语句中更新多个字段,但是字段之前有计算关系,同时更新时注意什么呢?
--------------------------------
比如一个表:
姓名,基本工资,奖金,补贴
aaaa,897,88,10
bbbb,1000,2000,50
cccc,2000,500,60我想实现,将奖金字段的值全部并入到基本工资中,让奖金列全为固定值20,补贴为原奖金的一半如何一条语句实现呢?
以前遇到过,这种情况,不好在同一条语句中处理?

解决方案 »

  1.   

    update 表名 set 基本工资=基本工资+奖金,奖金=20,补贴=奖金/2 where 条件
      

  2.   

    这只是一个例子,
    我想问共性的问题,就是一条update,改写多个字段,但是多个字段间有计算关系时,如何保证不出错,有什么原则呢?
      

  3.   

    create table a(a varchar(20),b int,c int,d int)
    insert a select 'aaaa',897,88,10
    union all select 'bbbb',1000,2000,50
    union all select 'cccc',2000,500,60update a set d=c/2,b=c+b,c=20select * from a
    --result
    /*a                    b           c           d           
    -------------------- ----------- ----------- ----------- 
    aaaa                 985         20          44
    bbbb                 3000        20          1000
    cccc                 2500        20          250(所影响的行数为 3 行)*/
      

  4.   


    declare @table table
    (
    姓名 varchar(10),
    基本工资 int not null,
    奖金 int not null,
    补贴 int not null
    )insert into @table 'aaaa',897,88,10 
    union all
    select 'bbbb',1000,2000,50
    union all
    select 'cccc',2000,500,60
    update @table set 基本工资=基本工资+奖金,奖金=20,补贴=奖金/2 where 姓名 in(select 姓名 from @table)