WHERE子句和IN运算符中的T-SQL CASE

时间:2022-06-27 11:51:40

I trying to do this query where I have a where clause. The problem is that I need to use inside the WHERE condition the operator IN.

我试图在我有一个where子句的地方进行查询。问题是我需要在WHERE条件内使用运算符IN。

Anyone can help me?

有人可以帮帮我吗?

Here is query. Thanks.

这是查询。谢谢。

DECLARE @title As Varchar(10)

SELECT * FROM EMPLOYEE
WHERE Title IN( CASE @title WHEN 'All' THEN  'FSOT','FSO','FISO','SFSO','PB','SPB' ELSE @title END)

2 个解决方案

#1


1  

No need for case expression, just use boolean logic:

不需要case表达式,只需使用布尔逻辑:

SELECT *
FROM EMPLOYEE
WHERE (@title = 'All'
      AND title IN ('FSOT', 'FSO', 'FISO', 'SFSO', 'PB', 'SPB')
      )
  OR (@title <> 'All'
      AND title = @title
      )

#2


0  

You can achieve this behavior with the logical or operator:

您可以使用逻辑或运算符实现此行为:

SELECT * 
FROM   employee
WHERE  (@title = 'All' AND Title IN ('FSOT', 'FSO', 'FISO', 'SFSO', 'PB', 'SPB')) OR
       (@title != 'All' AND Title = @title)

#1


1  

No need for case expression, just use boolean logic:

不需要case表达式,只需使用布尔逻辑:

SELECT *
FROM EMPLOYEE
WHERE (@title = 'All'
      AND title IN ('FSOT', 'FSO', 'FISO', 'SFSO', 'PB', 'SPB')
      )
  OR (@title <> 'All'
      AND title = @title
      )

#2


0  

You can achieve this behavior with the logical or operator:

您可以使用逻辑或运算符实现此行为:

SELECT * 
FROM   employee
WHERE  (@title = 'All' AND Title IN ('FSOT', 'FSO', 'FISO', 'SFSO', 'PB', 'SPB')) OR
       (@title != 'All' AND Title = @title)