简体   繁体   English

在SQL Server 2008中如何使用案例

[英]How to use case when in SQL Server 2008

I want to use case after where clause in SQL Server 2008; 我想在SQL Server 2008中where子句之后使用case how can I use it like 我该如何使用

select * 
from MPR 
where
    case when @min >= 0 
           then MainComponent_Id = @mainc and SubComponent_Id = @subcomp 
           else MainComponent_Id = @mainc and SubComponent_Id = @subcomp and MinorComponent_Id = @minorc   
   end
end

I am using a stored procedure, and in my stored procedure if minorcomponent_id value is 0 then it will add 1 one else 2 one will work. 我正在使用存储过程,并且在我的存储过程中,如果minorcomponent_id值为0,那么它将加1,否则2将起作用。

I had tried hard but its giving error in near case. 我已经尽力了,但是在某些情况下它给出了错误。

How to resolve it? 怎么解决呢?

Thanks 谢谢

You don't need to use a CASE statement. 您不需要使用CASE语句。 You can simplify logic as follows: 您可以简化逻辑,如下所示:

select 
    * 
from 
    MPR 
where
    MainComponent_Id = @mainc AND SubComponent_Id = @subcomp
    AND (@min >= 0 OR MinorComponent_Id = @minorc)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM