我希望能够在WHERE
子句中生成一个case表达式,该表达式将使用一个变量来确定要应用的标准:
declare @Classification VARCHAR(20)
set @Classification = 'Leaseup/Stabilized'
select *
from attributes a
where
if @Classification = 'Leaseup/Stabilized' then a.subgroup8 in ('Lease-up Communities', 'Stabilized
Communities')
if @Classification = 'Product Type' then a.subgroup6 in ('Freestanding AL/ALZ', 'Rental Continuums')
只需使用常规逻辑:
select a.*
from attributes a
where (@Classification = 'Leaseup/Stabilized' and a.subgroup8 in ('Lease-up Communities', 'Stabilized Communities'
) OR
(@Classification = 'Product Type' and a.subgroup6 in ('Freestanding AL/ALZ', 'Rental Continuums')
)
如果您真的想要Case
在中
declare @Classification
VARCHAR(20)
set @Classification =
'Leaseup/Stabilized'
select *
from attributes a
where
1= max (Case when @Classification =
'Leaseup/Stabilized' and a.subgroup8 in
('Lease-up Communities', 'Stabilized
Communities') then 1 end) or
1= max (Case when @Classification =
'Product Type' and a.subgroup6 in
('Freestanding AL/ALZ', 'Rental
Continuums') then 1 end)