update问题 存储过程

基础表 TABLE1
accout a b c d e
user1 100 90 80 70 60
user2 1000 900 800 700 600
user3 10000 9000 8000 7000 6000

更新表 TABLE2
accout a b c d e
user1 81 61
user2 1001 901
user3 10001 9001 6001

结果 更新后的TABLE1
accout a b c d e
user1 100 90 81 70 60
user2 1001 901 800 700 600
user3 10001 9001 8000 7000 6001

请教大家一个问题,我想用更新表TABLE2去更新基础表TABLE1的数据,得到的结果是更新后的TABLE1

但是有个问题,就是不清楚更新表TABLE2中哪些字段做了修改,从表上看非空的字段都是要更新到TABLE1去的

请教大家,谢谢
空的字段不更新

第1个回答  2012-04-07
mssql这么写:
update TABLE1
set
a=isnull(TABLE2.a,TABLE1.a),
b=isnull(TABLE2.b,TABLE1.b),
c=isnull(TABLE2.c,TABLE1.c),
d=isnull(TABLE2.d,TABLE1.d),
e=isnull(TABLE2.e,TABLE1.e),
from
TABLE1,TABLE2
where TABLE1.account=TABLE2.account
第2个回答  2012-04-05
要用函数比较麻烦 给你个思路
1.遍历table2 判断一行不为空值的字段
2.更新这些字段到table1

update 的时候 set 值的时候判断给的值是不是空 如果是 就给原值也就是table1的值 否则就更新为table2的值.
第3个回答  2012-04-05
----oracle的写法
update table1 t1
set (a,b,c,d,e) = (select nvl(t2.a,t1.a),nvl(t2.b,t1.b),nvl(t2.c,t1.c),nvl(t2.d,t1.d),nvl(t2.e,t1.e)
from table1 t2 where t1.account = t2.account);
第4个回答  2012-04-18
UPDATE TABLE1
SET table1.a=CASE WHEN table1.a=t2.a THEN table1.a ELSE ISNULL(t2.a,table1.a) END ,
table1.b=CASE WHEN table1.b=t2.b THEN table1.b ELSE ISNULL(t2.b,table1.b) END ,
table1.c=CASE WHEN table1.c=t2.c THEN table1.c ELSE ISNULL(t2.c,table1.c) END ,
table1.d=CASE WHEN table1.d=t2.d THEN table1.d ELSE ISNULL(t2.d,table1.d) END ,
table1.e=CASE WHEN table1.e=t2.e THEN table1.e ELSE ISNULL(t2.e,table1.e) END
FROM table2 t2
WHERE table1.accout=t2.accout本回答被提问者采纳