当前位置: 代码迷 >> Sql Server >> 请问SQL语句优化
  详细解决方案

请问SQL语句优化

热度:35   发布时间:2016-04-27 14:59:01.0
请教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)[email protected]    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)[email protected]_SL,January_TJE=isnull(January_TJE,0)[email protected]_JE where [email protected]_WLBM        Else if month(@VCRTTIME)=2                                                            ---2月份调整            update Y_LS set Feburary_TSL=isnull(Feburary_TSL,0)[email protected]_SL,Feburary_TJE=isnull(Feburary_TJE,0)[email protected]_JE where [email protected]_WLBM        Else if month(@VCRTTIME)=3                                                            ---3月份调整            update Y_LS set March_TSL=isnull(March_tSL,0)[email protected]_SL,March_TJE=isnull(March_TJE,0)[email protected]_JE where [email protected]_WLBM        Else if month(@VCRTTIME)=4                                                            ---4月份调整            update Y_LS set April_TSL=isnull(April_TSL,0)[email protected]_SL,April_TJE=isnull(April_TJE,0)[email protected]_JE where [email protected]_WLBM        Else if month(@VCRTTIME)=5                                                            ---5月份调整            update Y_LS set May_TSL=isnull(May_TSL,0)[email protected]_SL,May_TJE=isnull(May_TJE,0)[email protected]_JE where [email protected]_WLBM        Else if month(@VCRTTIME)=6                                                            ---6月份调整            update Y_LS set June_TSL=isnull(June_TSL,0)[email protected]_SL,June_TJE=isnull(June_TJE,0)[email protected]_JE where [email protected]_WLBM        Else if month(@VCRTTIME)=7                                                            ---7月份调整            update Y_LS set July_TSL=isnull(July_TSL,0)[email protected]_SL,July_TJE=isnull(July_TJE,0)[email protected]_JE where [email protected]_WLBM        Else if month(@VCRTTIME)=8                                                            ---8月份调整                update Y_LS set August_TSL=isnull(August_TSL,0)[email protected]_SL,August_TJE=isnull(August_TJE,0)[email protected]_JE where [email protected]_WLBM        Else if month(@VCRTTIME)=9                                                            ---9月份调整            update Y_LS set September_TSL=isnull(September_TSL,0)[email protected]_SL,September_TJE=isnull(September_TJE,0)[email protected]_JE where [email protected]_WLBM        Else if month(@VCRTTIME)=10                                                            ---10月份调整            update Y_LS set October_TSL=isnull(October_TSL,0)[email protected]_SL,October_TJE=isnull(October_TJE,0)[email protected]_JE where [email protected]_WLBM        Else if month(@VCRTTIME)=11                                                            ---11月份调整            update Y_LS set November_TSL=isnull(November_TSL,0)[email protected]_SL,November_TJE=isnull(November_TJE,0)[email protected]_JE where [email protected]_WLBM        Else if month(@VCRTTIME)=12                                                            ---12月份调整            update Y_LS set December_TSL=isnull(December_TSL,0)[email protected]_SL,December_TJE=isnull(December_TJE,0)[email protected]_JE where [email protected]_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)[email protected]join Y_LS c on b.WLBM=c.WLBMwhere datename(year,a.VCRTTIME)[email protected]
  相关解决方案