2016-06-27 66 views
1
update a_program 
set total_paguindikatif_rkpd = (select sum(isnull(pagu_dana_rkpd, 0)) 
           from a_kegiatan 
           where id_prog = @id_prog 
            and is_deleted = 0 
            and id_jenisanggaran = 2 
            and status in(1,3,7,9) 
           ), 
    total_paguindikatif_kuappas = (select sum(isnull(pagu_dana_kuappas, 0)) 
            from a_kegiatan 
            where id_prog = @id_prog 
            and is_deleted = 0 
            and id_jenisanggaran = 2 
            and status in(1,2,3,4,7,8,9,10) 
            ) 
where 
    id_prog = @id_prog and id_jenisanggaran = 2; 

這是我的查詢。我想在沒有loop @id_prog的情況下自動更新該查詢。我怎樣才能做到這一點?如何更新此查詢沒有循環或光標在SQL Server 2005中

感謝您幫助我.. :)

+0

正如旁白 - 您應該謹慎考慮您是否需要*商店*這些列在所有。一般來說,除非您有特定的性能需求(甚至在此之前,更喜歡使用內置的功能,例如自動維護派生數據的索引視圖),否則不要存儲*衍生的*數據 - 可以從其他數據計算的數據)。否則,你只是爲派生的數據引入一個過時/不符合實際的機會。 –

回答

2
update a set 
    total_paguindikatif_rkpd = (select sum(isnull(pagu_dana_rkpd, 0)) 
           from a_kegiatan 
           where id_prog = a.id_prog ---<<< 
            and is_deleted = 0 
            and id_jenisanggaran = 2 
            and status in(1,3,7,9) 
           ), 
    total_paguindikatif_kuappas = (select sum(isnull(pagu_dana_kuappas, 0)) 
            from a_kegiatan 
            where id_prog = a.id_prog ---<<< 
            and is_deleted = 0 
            and id_jenisanggaran = 2 
            and status in(1,2,3,4,7,8,9,10) 
            ) 
from a_program a 
where a.id_jenisanggaran = 2; 
+0

謝謝,Ivan Starostin :) 我會試試..老實說,我只是想知道答案:D –