试图在WHERE子句中进行大小写表达式(一个值等于多个)

问题描述 投票:0回答:1

我希望能够在WHERE子句中创建一个案例表达式,该表达式将使用变量来确定要应用的条件:

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')
sql sql-server ssms
1个回答
1
投票
只需使用常规逻辑:

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') )

© www.soinside.com 2019 - 2024. All rights reserved.