在存储过程中,我想实现循环统计,不晓得如何实现,麻烦好心人了:
select kc_bm from kw_tk_bk where xs_zxm=@xs_zxm and xs_sfz=@xs_sfz and zy_bm=@zy_bm
取出kc_bm比如为:01256 01245 01378
然后根据这个kc_bm搜索
Select kc_Money From jh_fa_kc_fy where kc_bm=@kc_bm
最后把这三个kc_bm对应的kc_money加起来返回给程序,如何实现?

解决方案 »

  1.   

    Select sum(m.kc_Money) From jh_fa_kc_fy m,kw_tk_bk n
    where m.kc_bm = n.kc_bm and n.xs_zxm=@xs_zxm and n.xs_sfz=@xs_sfz and n.zy_bm=@zy_bm
      

  2.   

    抱歉,望了个条件了,如果kc_bm不在jh_fa_kc_fy 中,则kc_Money为26,麻烦了
      

  3.   


    select sum(kc_Money) from
    (
    Select m.kc_Money From jh_fa_kc_fy m,kw_tk_bk n
    where m.kc_bm = n.kc_bm and n.xs_zxm=@xs_zxm and n.xs_sfz=@xs_sfz and n.zy_bm=@zy_bm
    union all
    Select kc_Money = 26 From jh_fa_kc_fy m where kc_bm not in (select kc_bm from kw_tk_bk n
    where n.xs_zxm=@xs_zxm and n.xs_sfz=@xs_sfz and n.zy_bm=@zy_bm)
    ) t
      

  4.   

    不对呀,大侠,Select kc_Money = 26 From jh_fa_kc_fy m where kc_bm not in (select kc_bm from kw_tk_bk n
    where n.xs_zxm=@xs_zxm and n.xs_sfz=@xs_sfz and n.zy_bm=@zy_bm 这个选择出来的太多了。
      

  5.   

    select sum(fy) as qr_money from
    (
    Select m.fy From jh_fa_kc_fy m,kw_tk_bk n
    where m.kc_bm = n.kc_bm and n.xs_sfz=@xs_sfz and n.zy_bm=@zy_bm
    union all
    Select fy = 26 From kw_tk_bk n where n.zy_bm=@zy_bm and n.xs_sfz=@xs_sfz
     and  n.kc_bm not in (select kc_bm from jh_fa_kc_fy)
    ) t
    这样就对了,谢谢你!!