首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

请问SQL语句优化

2012-03-27 
请教SQL语句优化SQL codedeclare mycursor cursor for select A.VCRTTIME,B.WLBM,B.DZSL,B.DZSL from KCTZ

请教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)=@datejoin Y_LS c on b.WLBM=c.WLBMwhere datename(year,a.VCRTTIME)=@date 


[解决办法]

SQL code
update c set january_TSL=case when month(A.VCRTTIME)=1 then isnull(January_TSL,0)+B.DZSL else january_TSL end, January_TJE=case when month(A.VCRTTIME)=1 then isnull(January_TJE,0)+B.DZSL else January_TJE end january_TSL=case when month(A.VCRTTIME)=1 then isnull(January_TSL,0)+B.DZSL else january_TSL end ..... ..... .....from KCTZ_2 B join KCTZ_1 A on A.vguid=B.vguid and datename(year,a.VCRTTIME)=@datejoin Y_LS c on b.WLBM=c.WLBMwhere datename(year,a.VCRTTIME)=@date
[解决办法]
...没黏贴完 .. 最后漏了个小东西
SQL code
UPDATE Y_LSSET January_TSL=case when   month(A.VCRTTIME)=1  then isnull(January_TSL,0)+B.WLBM end ,    January_TJE=case when  month(A.VCRTTIME)=1   then isnull(January_TJE,0)+B.DZSL end,    Feburary_TSL=case when  month(A.VCRTTIME)=2  then isnull(Feburary_TSL,0)+B.WLBM end ,    Feburary_TJE=case when  month(A.VCRTTIME)=2  then isnull(Feburary_TJE,0)+B.DZSL end,    ...    ...    December_TSL=case when month(A.VCRTTIME)=12 then isnull(December_TSL,0)+B.WLBM end ,    December_TJE=case when month(A.VCRTTIME)=12 then isnull(December_TJE,0)+B.DZSL endFROM KCTZ_2 B,KCTZ_1 A WHERE A.vguid=B.vguid and datename(year,a.VCRTTIME)=@date and Y_LS.WLBM=B.DZSL;
[解决办法]
用Case When更好,虽然也很长。尽可能不要使用游标,尤其是业务繁忙的表。

热点排行