日期:2014-05-18  浏览次数:20573 次

请教SQL语句优化
SQL code

declare mycursor cursor for select A.VCRTTIME,B.WLBM,B.DZSL,B.DZSL from KCTZ_2 B,KCTZ_1 A where A.vguid=B.vguid and datename(year,a.VCRTTIME)=@date
    open mycursor 
    fetch next from mycursor into @VCRTTIME,@RK_WLBM,@RK_SL,@RK_JE
    while @@fetch_status=0
    Begin 
        if  month(@VCRTTIME)=1                                                                ---1月份调整
            update Y_LS set January_TSL=isnull(January_TSL,0)+@RK_SL,January_TJE=isnull(January_TJE,0)+@RK_JE where WLBM=@RK_WLBM
        Else if month(@VCRTTIME)=2                                                            ---2月份调整
            update Y_LS set Feburary_TSL=isnull(Feburary_TSL,0)+@RK_SL,Feburary_TJE=isnull(Feburary_TJE,0)+@RK_JE where WLBM=@RK_WLBM
        Else if month(@VCRTTIME)=3                                                            ---3月份调整
            update Y_LS set March_TSL=isnull(March_tSL,0)+@RK_SL,March_TJE=isnull(March_TJE,0)+@RK_JE where WLBM=@RK_WLBM
        Else if month(@VCRTTIME)=4                                                            ---4月份调整
            update Y_LS set April_TSL=isnull(April_TSL,0)+@RK_SL,April_TJE=isnull(April_TJE,0)+@RK_JE where WLBM=@RK_WLBM
        Else if month(@VCRTTIME)=5                                                            ---5月份调整
            update Y_LS set May_TSL=isnull(May_TSL,0)+@RK_SL,May_TJE=isnull(May_TJE,0)+@RK_JE where WLBM=@RK_WLBM
        Else if month(@VCRTTIME)=6                                                            ---6月份调整
            update Y_LS set June_TSL=isnull(June_TSL,0)+@RK_SL,June_TJE=isnull(June_TJE,0)+@RK_JE where WLBM=@RK_WLBM
        Else if month(@VCRTTIME)=7                                                            ---7月份调整
            update Y_LS set July_TSL=isnull(July_TSL,0)+@RK_SL,July_TJE=isnull(July_TJE,0)+@RK_JE where WLBM=@RK_WLBM
        Else if month(@VCRTTIME)=8                                                            ---8月份调整    
            update Y_LS set August_TSL=isnull(August_TSL,0)+@RK_SL,August_TJE=isnull(August_TJE,0)+@RK_JE where WLBM=@RK_WLBM
        Else if month(@VCRTTIME)=9                                                            ---9月份调整
            update Y_LS set September_TSL=isnull(September_TSL,0)+@RK_SL,September_TJE=isnull(September_TJE,0)+@RK_JE where WLBM=@RK_WLBM
        Else if month(@VCRTTIME)=10                                                            ---10月份调整
            update Y_LS set October_TSL=isnull(October_TSL,0)+@RK_SL,October_TJE=isnull(October_TJE,0)+@RK_JE where WLBM=@RK_WLBM
        Else if month(@VCRTTIME)=11                                                            ---11月份调整
            update Y_LS set November_TSL=isnull(November_TSL,0)+@RK_SL,November_TJE=isnull(November_TJE,0)+@RK_JE where WLBM=@RK_WLBM
        Else if month(@VCRTTIME)=12                                                            ---12月份调整
            update Y_LS set December_TSL=isnull(December_TSL,0)+@RK_SL,December_TJE=isnull(December_TJE,0)+@RK_JE where WLBM=@RK_WLBM                 
        fetch next from mycursor into @VCRTTIME,@RK_WLBM,@RK_SL,@RK_JE
    End
    close mycursor 
    deallocate mycursor




------解决方案--------------------
我承认我看的很晕....
------解决方案--------------------
改改Y_LS表结构,会省事的多。
------解决方案--------------------
SQL code
update c
 set january_TSL=case when month(A.VCRTTIME)=1 then isnull(January_TSL,0)+B.DZSL,
 January_TJE=case when month(A.VCRTTIME)=1 then isnull(January_TJE,0)+B.DZSL
 january_TSL=case when month(A.VCRTTIME)=1 then isnull(January_TSL,0)+B.DZSL
 .....
 .....
 .....
from KCTZ_2 B join KCTZ_1 A on A.vguid=B.vguid and datename(year,a.VCRTTIME)=@date
join Y_LS c on b.WLBM=c.WLBM
where date