我们如何在WHERE子句中从CASE指定值的范围?
我的此查询失败

declare @ProductType int

select * from Products
where ProductLine in
(
    case @ProductType
        when 1 then ('TVs')
        when 2 then ('Handsets', 'Mobiles') --fails here
        else ('Books')
    end
)

这也不起作用:
declare @ProductType int

select * from Products
where (case @ProductType
             when 1 then (ProductLine = 'TVs')
             when 2 then (ProductLine in  ('Handsets', 'Mobiles'))
             else (ProductLine = 'Books')
       end)

最佳答案

您无法做到这一点-您需要将其拆分为几项检查:

WHERE (ProductLine = 'TVs' AND @ProductType = 1)
   OR (ProductLine IN ('Handsets', 'Mobiles') AND @ProductType = 2)
   OR (ProductLine = 'Books' AND @ProductType NOT IN (1, 2))

10-07 23:44