with r as { select CLM001 ,sum(score) as total from b group by CLM001 } update a set score = (select total from r where r.CLM001 =a.CLM001) where exists (select 1 from r where r.CLM001 =a.CLM001)
點評:此方法需要寫存儲過程,否則在程序中沒法用。
create or replace PRocedure p_test as v_clm001 a.clm001%type; cursor c_record is select distinct clm001 from b; begin open c_record; loop fetch c_record into v_clm001; exit when c_record%notfound; update a set score =(select sum(score) from b where clm001=v_clm001) where clm001=v_clm001; end loop; end;
點評:這是在PL/SQL中的語句,此問題用PL/SQL解決也不失為一個簡單的方法。
update a set a.score = a.score + (select b.score from a a, (select CLM001,sum(score) from b group by CLM001) b where a.CLM001 = b.CLM001)