我在SQL Server 2005中使用游标是对还是错?

时间:2011-09-28 06:01:26

标签: sql-server-2005

在aspx页面我有两个下拉列表,分别是节日下拉列表和年份下拉列表,当用户选择一个值在节日如圣诞节和年份下拉列表假设2011年,所以时间所有电影细节应该在asp看这就是为什么我在SQL Server 2005中使用游标但有一些条件的原因

  1. 用户可以选择年份下拉列表值“全部”节日应该是个人选择
  2. 年应该是个人和场合个人
  3. 年应该是个人和场合个人
  4. 第三个条件我已经完成但是第一和第二个条件我怎么能不做...?

    set ANSI_NULLS ON
    set QUOTED_IDENTIFIER ON
    GO
    
    ALTER procedure [dbo].[Sp_OccasionMovieDetails]
    
    @OrderBy varchar(100),
    @year_val varchar(50),
    @occasion_val varchar(50)
    As
    
    
     --[Sp_OccasionMovieDetails] 'Release_Date', '2011', '0'
    
    Set nocount on
    
    declare @Curr_Contri_1 int;
    declare @Curr_Contri_2 int;
    declare @Curr_Contri_3 int;
    declare @Curr_Contri_4 int;
    declare @Percent_2 bigint;
    declare @Percent_3 bigint;
    declare @Percent_4 bigint;
    declare @Class_2 varchar(100);
    declare @Class_3 varchar(100);
    declare @Class_4 varchar(100);
    declare @Id_Movie varchar(100);
    declare @Release_Date datetime;
    declare @MovieTitle varchar(100);
    --declare @Id_festival1 varchar(100);
    declare @id_festival_new varchar(50);
    declare @fromdate varchar(50);
    declare @todate varchar(50);
    declare @festivalname varchar(50);
    
    --Year All and ocassion Individual selected
    
    if(@year_val='0' and @occasion_val<>'0')
    begin
    
        print '1'
    
    end
    
    
    ----year individual and ocassion individual selected
    
    else if (@year_val<>'0' and @Occasion_val<>'0')
    begin 
        print '2'
    
        declare List cursor for
        select md.id_festival, md.fromdate, md.todate, mf.festivalname  from BTNN_MovieDB_Festival_Details md, BTNN_MovieDB_Festival mf where md.id_festival=mf.id_festival and md.syncoperation<>'D' and mf.syncoperation<>'D' and year(md.fromdate)=@year_val order by mf.festivalname
        OPEN List 
        FETCH NEXT FROM List INTO @id_festival_new, @fromdate, @todate, @festivalname
        WHILE @@FETCH_STATUS = 0
        BEGIN
    
            select @id_festival_new as id_festival, @fromdate as fromdate, @todate as todate, @festivalname as festivalname, 'occasion' as flag
    
            declare currMovieYearMain cursor for
            select top 25 id_movie ,movietitle, release_date 
            from (
                    select id_movie ,movietitle, release_date 
                    from View_Territory
                    where (release_date between @fromdate and @todate)and territorycat=0
                    group by id_movie ,MovieTitle, release_date
            ) as x
                    ORDER BY CASE WHEN @OrderBy = 'MovieTitle' THEN movietitle end,
                             CASE WHEN @OrderBy = 'ReleaseDate' THEN release_date end desc
    
            open currMovieYearMain
                fetch next from currMovieYearMain into @Id_movie,@MovieTitle,@Release_Date
                while @@fetch_status=0
            begin
                select @Curr_Contri_1=sum(territory_total) from view_territory where territorycat=0
                and id_movie=@Id_movie and week=1
                set @Curr_Contri_1=isnull(@Curr_Contri_1,0)
    
                select @Curr_Contri_2=sum(territory_total) from view_territory where territorycat=0
                and id_movie=@Id_movie and week=2
                set @Curr_Contri_2=isnull(@Curr_Contri_2,0)
    
                if @Curr_Contri_1 is not null and @Curr_Contri_1<>0 and @Curr_Contri_2<>0
            begin
                set @Class_2='green'
                set @Percent_2=(cast(@Curr_Contri_2 as bigint)*100)/@Curr_Contri_1
    
                if @Percent_2>0
            begin
                set @Percent_2=@Percent_2-100
            end
                else
            begin
                set @Percent_2=0
            end
    
                if @Curr_Contri_1>@Curr_Contri_2
            begin
                set @Percent_2=0-@Percent_2
                set @Class_2='redsmall'
            end
    
                else if @Curr_Contri_1=@Curr_Contri_2
            begin
                set @Percent_2=0
                set @Class_2=null
            end
            end
                else
            begin
                set @Percent_2=0
                set @Class_2=null
            end
    
                select @Curr_Contri_3=sum(territory_total) from view_territory where territorycat=0
                and id_movie=@Id_movie and week=3
                set @Curr_Contri_3=isnull(@Curr_Contri_3,0)
    
                if @Curr_Contri_3=isnull(@Curr_Contri_3,0)
    
                if @Curr_Contri_2 is not null and @Curr_Contri_2 <> 0 and @Curr_Contri_3 <> 0
            begin
                set @Class_3='green'
                set @Percent_3=(cast(@Curr_Contri_3 as bigint)*100)/@Curr_Contri_2
    
                if @Percent_3 > 0
            begin
                set @Percent_3=@Percent_3-100
            end
                else 
            begin
                set @Percent_3=0
            end
                if @Curr_Contri_2 > @Curr_Contri_3
            begin
                set @Percent_3=0-@Percent_3
                set @Class_3='redsmall'
            end 
                else if @Curr_Contri_2=@Curr_Contri_3
            begin
                set @Percent_3=0
                set @Class_3=null
            end
            end
                else
            begin
                set @Percent_3=0
                set @Class_3=null
            end
    
                select @Curr_Contri_4=sum(territory_total) from View_Territory where territorycat=0
                and id_movie=@Id_Movie and week=4
                set @Curr_Contri_4=isnull(@Curr_Contri_4,0)
    
                if @Curr_Contri_3 is not null and @Curr_Contri_3 <>0 and @Curr_Contri_4 <> 0
            begin
                set @Class_4='green'
                set @Percent_4=(cast(@Curr_Contri_4 as bigint)*100)/@Curr_Contri_3
    
                if @Percent_4 >0
            begin
                set @Percent_4=@Percent_4-100
            end
    
                else
            begin
                set @Percent_4=0
            end
    
                if @Curr_Contri_3>@Curr_Contri_4
            begin
                set @Percent_4=0-@Percent_4
                set @Class_4='redsmall'
            end
    
                else if @Curr_Contri_3=@Curr_Contri_4
            begin
                set @Percent_4=0
                set @Class_4=null
            end
            end
    
                else 
            begin
                set @Percent_4=0
                set @Class_4=null
            end
    
                select @Id_Movie as Id_Movie, @MovieTitle as MovieTitle,@Curr_Contri_1 as Curr_Contri_1, @Curr_Contri_2 as Curr_Contri_2, 
                @Class_2 as Class_2, @Percent_2 as PerChange_2, @Curr_Contri_3 as Curr_Contri_3,
                @Class_3 as Class_3,@Percent_3 as PerChange_3, @Curr_Contri_4 as Curr_Contri_4,
                @Class_4 as Class_4,@Percent_4 as PerChange_4, @Release_Date as Release_Date, 'movies' as flag
    
                fetch next from currMovieYearMain into @Id_Movie,@Movietitle,@Release_Date
            End
                close CurrMovieYearMain
                Deallocate CurrMovieYearMain
    
            FETCH NEXT FROM List INTO @id_festival_new, @fromdate, @todate, @festivalname
        END
        CLOSE List
        DEALLOCATE List 
    end
    
    
    
    ----- Year Individual and ocassion All selected
    else if(@year_val<>'0' and @Occasion_val='0')
    begin
    
        print '3'
    
        declare List cursor for
        select md.id_festival, md.fromdate, md.todate, mf.festivalname  from BTNN_MovieDB_Festival_Details md, BTNN_MovieDB_Festival mf where md.id_festival=mf.id_festival and md.syncoperation<>'D' and mf.syncoperation<>'D' and year(md.fromdate)=@year_val order by mf.festivalname
        OPEN List 
        FETCH NEXT FROM List INTO @id_festival_new, @fromdate, @todate, @festivalname
        WHILE @@FETCH_STATUS = 0
        BEGIN
    
            select @id_festival_new as id_festival, @fromdate as fromdate, @todate as todate, @festivalname as festivalname, 'occasion' as flag
    
            declare currMovieYearMain cursor for
            select top 25 id_movie ,movietitle, release_date 
            from (
                    select id_movie ,movietitle, release_date 
                    from View_Territory
                    where (release_date between @fromdate and @todate) and territorycat=0
                    group by id_movie ,MovieTitle, release_date
            ) as x
                    ORDER BY CASE WHEN @OrderBy = 'MovieTitle' THEN movietitle end,
                             CASE WHEN @OrderBy = 'ReleaseDate' THEN release_date end desc
    
            open currMovieYearMain
                fetch next from currMovieYearMain into @Id_movie,@MovieTitle,@Release_Date
                while @@fetch_status=0
            begin
                select @Curr_Contri_1=sum(territory_total) from view_territory where territorycat=0
                and id_movie=@Id_movie and week=1
                set @Curr_Contri_1=isnull(@Curr_Contri_1,0)
    
                select @Curr_Contri_2=sum(territory_total) from view_territory where territorycat=0
                and id_movie=@Id_movie and week=2
                set @Curr_Contri_2=isnull(@Curr_Contri_2,0)
    
                if @Curr_Contri_1 is not null and @Curr_Contri_1<>0 and @Curr_Contri_2<>0
            begin
                set @Class_2='green'
                set @Percent_2=(cast(@Curr_Contri_2 as bigint)*100)/@Curr_Contri_1
    
                if @Percent_2>0
            begin
                set @Percent_2=@Percent_2-100
            end
                else
            begin
                set @Percent_2=0
            end
    
                if @Curr_Contri_1>@Curr_Contri_2
            begin
                set @Percent_2=0-@Percent_2
                set @Class_2='redsmall'
            end
    
                else if @Curr_Contri_1=@Curr_Contri_2
            begin
                set @Percent_2=0
                set @Class_2=null
            end
            end
                else
            begin
                set @Percent_2=0
                set @Class_2=null
            end
    
                select @Curr_Contri_3=sum(territory_total) from view_territory where territorycat=0
                and id_movie=@Id_movie and week=3
                set @Curr_Contri_3=isnull(@Curr_Contri_3,0)
    
                if @Curr_Contri_3=isnull(@Curr_Contri_3,0)
    
                if @Curr_Contri_2 is not null and @Curr_Contri_2 <> 0 and @Curr_Contri_3 <> 0
            begin
                set @Class_3='green'
                set @Percent_3=(cast(@Curr_Contri_3 as bigint)*100)/@Curr_Contri_2
    
                if @Percent_3 > 0
            begin
                set @Percent_3=@Percent_3-100
            end
                else 
            begin
                set @Percent_3=0
            end
                if @Curr_Contri_2 > @Curr_Contri_3
            begin
                set @Percent_3=0-@Percent_3
                set @Class_3='redsmall'
            end 
                else if @Curr_Contri_2=@Curr_Contri_3
            begin
                set @Percent_3=0
                set @Class_3=null
            end
            end
                else
            begin
                set @Percent_3=0
                set @Class_3=null
            end
    
                select @Curr_Contri_4=sum(territory_total) from View_Territory where territorycat=0
                and id_movie=@Id_Movie and week=4
                set @Curr_Contri_4=isnull(@Curr_Contri_4,0)
    
                if @Curr_Contri_3 is not null and @Curr_Contri_3 <>0 and @Curr_Contri_4 <> 0
            begin
                set @Class_4='green'
                set @Percent_4=(cast(@Curr_Contri_4 as bigint)*100)/@Curr_Contri_3
    
                if @Percent_4 >0
            begin
                set @Percent_4=@Percent_4-100
            end
    
                else
            begin
                set @Percent_4=0
            end
    
                if @Curr_Contri_3>@Curr_Contri_4
            begin
                set @Percent_4=0-@Percent_4
                set @Class_4='redsmall'
            end
    
                else if @Curr_Contri_3=@Curr_Contri_4
            begin
                set @Percent_4=0
                set @Class_4=null
            end
            end
    
                else 
            begin
                set @Percent_4=0
                set @Class_4=null
            end
    
                select @Id_Movie as Id_Movie, @MovieTitle as MovieTitle,@Curr_Contri_1 as Curr_Contri_1, @Curr_Contri_2 as Curr_Contri_2, 
                @Class_2 as Class_2, @Percent_2 as PerChange_2, @Curr_Contri_3 as Curr_Contri_3,
                @Class_3 as Class_3,@Percent_3 as PerChange_3, @Curr_Contri_4 as Curr_Contri_4,
                @Class_4 as Class_4,@Percent_4 as PerChange_4, @Release_Date as Release_Date, 'movies' as flag
    
                fetch next from currMovieYearMain into @Id_Movie,@Movietitle,@Release_Date
            End
                close CurrMovieYearMain
                Deallocate CurrMovieYearMain
    
            FETCH NEXT FROM List INTO @id_festival_new, @fromdate, @todate, @festivalname
        END
        CLOSE List
        DEALLOCATE List 
    
    end
    

0 个答案:

没有答案