使用CASE,在T-SQL IN子句 [英] Using a CASE with the IN clause in T-SQL

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

问题描述

在我的WHERE子句我使用的情况下将返回所有行,如果参数为空或空。这工作得很好单valuses。但是没有这么好使用IN子句时。例如:

In My WHERE Clause I am using a CASE that will return all rows if parameter is blank or null. This works fine with single valuses. However not so well when using the IN clause. For example:

这工作得很好,如果没有匹配,则返回所有的记录!

This works very well, if there is not match then ALL records are returned!

AND 
Name = CASE WHEN @Name_ != '' THEN @Name_ ELSE Name END
AND

这个作品也,但没有办法使用CASE前pression,所以如果我不提供额外的价值,我将看不到任何记录:

This works also, but there is no way to use the CASE expression, so if i do not provide some value i will not see any records:

AND
Name IN (Select Names FROM Person Where Names like @Name_)
AND 

我能用第二个例子呢?所以,如果有不匹配的所有名称将被退回?

Can I use a CASE with the 2nd example? So if there is not a match all Names will be returned?

推荐答案

也许合并将解决您的问题。

AND
Name IN (Select Names FROM Person Where Names like coalesce(@Name,Name))
AND 

由于Mattfew莱克说和使用,你也可以使用 ISNULL 函数

Name IN (Select Names FROM Person Where Names like isnull(@Name,Name)) 

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

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