在 IN 子句中使用 CASE 语句 [英] Using CASE Statement inside IN Clause

查看:52
本文介绍了在 IN 子句中使用 CASE 语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在 IN 子句中使用 CASE 语句?

Is is possible to use a CASE statement inside an IN clause?

这是我一直试图正确编译的简化版本:

This is a simplified version of what I have been trying to get to compile correctly:

SELECT * FROM MyTable 
WHERE StatusID IN (
CASE WHEN @StatusID = 99 THEN (5, 11, 13)
ELSE (@StatusID) END )

谢谢!

推荐答案

CASE 仅返回标量值.你可以这样做.(我假设,根据您的示例,当 @StatusID = 99 时,StatusID 值 99 不匹配.)

CASE returns a scalar value only. You can do this instead. (I am assuming, as per your example, that when @StatusID = 99, a StatusID value of 99 is not a match.)

select *
from MyTable
where (@StatusID = 99 and StatusID in (5, 11, 13))
    or (@StatusID <> 99 and StatusID = @StatusID)

这篇关于在 IN 子句中使用 CASE 语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆