马上就开始了 - 我很陌生。我阅读了以下内容:How do I perform an IF...THEN in an SQL SELECT?但它并没有真正回答我的问题。
基本上我正在尝试做的事情如下:
select
section_name, *
from
property.lease_period lp
where
lp.lease_current_stop_date < getdate() and (lp.lease_status = 'Active' or lp.lease_status = 'Overholding')
and lp.period_id = @period_id
and lp.building_id = @building_id
and not exists
(
select 1
from lease_deal.lease
where lp.suite_name = tenancy_reference
and lp.building_id = building_id
)
case when(@section_name <> 'ALL')
then(and upper(section_name) = upper(@section_name))
end
order by period_id desc
这可能吗?如果是这样,我做错了什么?
铊; DR:
基本上我想:
and upper(section_name) = upper(@section_name)
仅在@section_name不等于'ALL'时才适用于我的where子句
答案 0 :(得分:3)
您可以将您的(非工作)CASE更改为
AND (@section_name = 'ALL' OR upper(section_name) = upper(@section_name))
答案 1 :(得分:1)
这可以通过更简单的方式完成,而无需使用CASE。它将是这样的:
and ((upper(section_name) = upper(@section_name) and @section_name <> 'ALL') OR @section_name ='ALL')
答案 2 :(得分:0)
AND upper(section_name)=CASE WHEN @section_name <> 'ALL' THEN upper(@section_name)
ELSE upper(section_name)
END