如果不满足条件,则获取包括NULL在内的所有值 [英] WHERE If condition is not met get all values including NULL
本文介绍了如果不满足条件,则获取包括NULL在内的所有值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
要么仅获取具有指定@Lastname的那些用户ID,要么如果@LastName ='All',则即使它们的姓氏为NULL,也获取所有用户ID.
Either get only those userIDs with the specified @Lastname or if @LastName = 'All' then get all userIDs even if they have NULL values as their LastName.
SELECT userID INTO #table
FROM users
WHERE LastName = CASE
WHEN @LastName = 'All' THEN LastName
ELSE @LastName END
上面的查询仅返回LastName不为NULL的那些用户ID.
The above query only returns those userIDs where the LastName is not NULL.
推荐答案
另一个简短形式
where @LastName in ('All', LastName);
这篇关于如果不满足条件,则获取包括NULL在内的所有值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文