-1 sql stored-procedures sql-server-2008
我想在SQL Server 2008中使用caseafter where子句; 我怎么能像它一样使用它
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
Run Code Online (Sandbox Code Playgroud)
我正在使用存储过程,并且在我的存储过程中,如果minorcomponent_id值为0,那么它将添加1,否则其中2将起作用.
我曾经努力过,但在近乎情况下给出了错误.
怎么解决?
谢谢
您不需要使用CASE语句.您可以按如下方式简化逻辑:
select
*
from
MPR
where
MainComponent_Id = @mainc AND SubComponent_Id = @subcomp
AND (@min >= 0 OR MinorComponent_Id = @minorc)
Run Code Online (Sandbox Code Playgroud)