如何在存储过程中的where子句之后使用if else条件

时间:2014-09-18 11:04:54

标签: sql sql-server database sql-server-2008

我想在where子句之后使用if条件我该如何使用它。我试过但是有错误。

ALTER proc [dbo].[finprog] @finmnth int,@finyear nvarchar(50),@mainc int,@subcomp int,@minorc int,@divid int
    as 
    declare @min int

    begin
    if @finmnth=3 or @finmnth=2 or @finmnth=1
    set @min=13
     else
     set @min=@finmnth
    if @minorc>0 
    select 
/* Progress till Lst year*/
    isNull(CONVERT(DECIMAL(10,3),SUM(case when  mpryear<@finyear then (IDA+Govt)/100000 else 0 end),2),0)as FPPC,
    isNull(CONVERT(DECIMAL(10,3),SUM(case when  mpryear<@finyear then (Benyfe)/100000 else 0 end),2),0)as FPBC,
    /* Progress till Lst month*/
    case when @finmnth=3 OR @finmnth=2 then
    isNull(CONVERT(DECIMAL(10,3),SUM(case when month<@finmnth and mpryear=@finyear then (IDA+Govt)/100000 else 0 end),2),0)
    end as PtLm,
    isNull(CONVERT(DECIMAL(10,3),SUM(case when month between 4 and @min-1 and mpryear=@finyear then (IDA+Govt)/100000 else 0 end),2),0)as Ptlm1,
    case when @finmnth=3 OR @finmnth=2 then
    isNull(CONVERT(DECIMAL(10,3),SUM(case when month<@finmnth and mpryear=@finyear then (Benyfe)/100000 else 0 end),2),0)
    end as PtLmBe,
    isNull(CONVERT(DECIMAL(10,3),SUM(case when month between 4 and @min-1 and mpryear=@finyear then (Benyfe)/100000 else 0 end),2),0)as PtlmBe1,

    /* Progressduring month*/
    isNull(CONVERT(DECIMAL(10,3),SUM(case when month=@finmnth and mpryear=@finyear then (IDA+Govt)/100000 else 0 end),2),0)as PUDMPc,
    isNull(CONVERT(DECIMAL(10,3),SUM(case when month=@finmnth and mpryear=@finyear then (Benyfe)/100000 else 0 end),2),0)as PUDMBe,
    /* Cummulative Progress */
    isNull(CONVERT(DECIMAL(10,3),SUM(case when  mpryear<=@finyear then (IDA+Govt)/100000 else 0 end),2),0)as CUTMPc,
    isNull(CONVERT(DECIMAL(10,3),SUM(case when  mpryear<=@finyear then (Benyfe)/100000 else 0 end),2),0)as CUTMBe,
    isNull(CONVERT(DECIMAL(10,3),SUM(case when  mpryear<=@finyear then (IDA+Govt+Benyfe)/100000 else 0 end),2),0)as CUTMTot,
    /* IDA+Govt+Bene*/
    isNull(CONVERT(DECIMAL(10,3),SUM(case when  mpryear<=@finyear then (IDA)/100000 else 0 end),2),0)as IDA,
    isNull(CONVERT(DECIMAL(10,3),SUM(case when  mpryear<=@finyear then (Govt)/100000 else 0 end),2),0)as Gvt,
    isNull(CONVERT(DECIMAL(10,3),SUM(case when  mpryear<=@finyear then (Benyfe)/100000 else 0 end),2),0)as Bene,
    (select isNull(CONVERT(DECIMAL(10,3),SUM(case when  awpyear=@finyear then (IDA+Govt)/100000 else 0 end),2),0) as atPC from AWP where MainComponent_Id=@mainc and SubComponent_Id=@subcomp AND  MinorComponent_Id = @minorc )as ATPc,
    (select isNull(CONVERT(DECIMAL(10,3),SUM(case when  awpyear=@finyear then (Benyfe)/100000 else 0 end),2),0)as CUTMBe  from AWP where MainComponent_Id=@mainc and SubComponent_Id=@subcomp AND  MinorComponent_Id = @minorc ) as ATBe,
    (select isNull(CONVERT(DECIMAL(10,3),SUM(case when  awpyear=@finyear then (IDA+Govt+Benyfe)/100000 else 0 end),2),0)as CUTMTot  from AWP where MainComponent_Id=@mainc and SubComponent_Id=@subcomp AND  MinorComponent_Id = @minorc )as ATTot
    from 

    MPR  where 
    if @divid=0  
    MainComponent_Id=@mainc and SubComponent_Id=@subcomp AND  MinorComponent_Id = @minorc 
    else
    MainComponent_Id=@mainc and SubComponent_Id=@subcomp AND  MinorComponent_Id = @minorc  and division=@divid
    end 

3 个答案:

答案 0 :(得分:1)

你可以使用它

   where 
    (@divid=0  and
    MainComponent_Id=@mainc and SubComponent_Id=@subcomp AND  MinorComponent_Id = @minorc )
    or(@divid<>0 and
    MainComponent_Id=@mainc and SubComponent_Id=@subcomp AND  MinorComponent_Id = @minorc  and division=@divid)

答案 1 :(得分:1)

您可以在where子句中使用CASE。根据您的特殊要求,您可以将where子句写为

 where 
MainComponent_Id=@mainc and SubComponent_Id=@subcomp AND  MinorComponent_Id = @minorc  and 
division=(CASE @divid WHEN 0 THEN division ELSE @divid END)

答案 2 :(得分:0)

这样做: WHERE(MainComponent_Id = @ mainc和SubComponent_Id = @ subcomp AND MinorComponent_Id = @minorc AND(division = @Divid OR @divid = 0))