如何在WHERE子句中格式化此CASE表达式?

时间:2023-01-01 20:13:11

I'm trying to set the WHERE clause to query ALL urgency levels (1,2,3,4) when "All" is selected.

我正在尝试设置WHERE子句以在选择“全部”时查询所有紧急级别(1,2,3,4)。

The values for @Priority is: 1 - (Critical) 2 - (High) 3 - (Medium) 4 - (Low) All - (All Priorities)

@Priority的值为:1 - (严重)2 - (高)3 - (中)4 - (低)全部 - (所有优先级)

....

WHERE Facility = 'HQ'
 AND Urgency = CASE WHEN @Priority = 'All' THEN IN ('1','2','3','4')
    ELSE Urgency = @Priority
END

=================

So, if '1' is selected as @Priority, then Urgency = 1, if '2' is selected, then Urgency = 2. If 'All' is selected, THEN "Urgency IN ('1','2','3','4')".

因此,如果选择'1'作为@Priority,则紧急度= 1,如果选择'2',则紧急度= 2.如果选择'全部',那么“紧急情况IN('1','2',' 3' , '4')”。

Is there a way to do this?
Thanks!

有没有办法做到这一点?谢谢!

1 个解决方案

#1


1  

Try this:

 WHERE 
    Facility = 'HQ' AND
    Urgency = CASE WHEN @Priority = 'All' THEN Urgency
                   ELSE @Priority END

#1


1  

Try this:

 WHERE 
    Facility = 'HQ' AND
    Urgency = CASE WHEN @Priority = 'All' THEN Urgency
                   ELSE @Priority END