首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >检查列表是否为空或空,然后检查SQL。

检查列表是否为空或空,然后检查SQL。
EN

Stack Overflow用户
提问于 2016-03-15 08:48:41
回答 3查看 10.8K关注 0票数 2

我有这样的疑问,

代码语言:javascript
复制
with t as (
      <your query here>
     )
select t.id, t.name, t.randomid, trand.name as randomname
from t left join
     t trand
     on t.randomid = trand.id
where t.id in (select item from dbo.ufnSplit(@ids,','));

Here is code for ufnSplit

如何添加检查,以便如果@ids为空或空,然后使用where条件,只有在@ids不为空或空时,才需要此条件?

代码语言:javascript
复制
where t.id in (select item from dbo.ufnSplit(@ids,','));
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2016-03-15 08:55:15

这将检查@ if是空还是空,然后检查where子句。

代码语言:javascript
复制
with t as (
      <your query here>
     )
select t.id, t.name, t.randomid, trand.name as randomname
from t left join
     t trand
     on t.randomid = trand.id
where @ids = '' 
   or @ids is null
   or t.id in (select item from dbo.ufnSplit(@ids,','))
票数 3
EN

Stack Overflow用户

发布于 2016-03-15 08:51:40

试试这个Where子句

代码语言:javascript
复制
where t.id in (select item from dbo.ufnSplit(@ids,',')) or nullif(@ids,'') is null;
票数 1
EN

Stack Overflow用户

发布于 2016-03-15 08:51:49

您只需使用一个条件WHERE子句,如下所示:

代码语言:javascript
复制
with t as (
      <your query here>
     )
select t.id, t.name, t.randomid, trand.name as randomname
from t left join
     t trand
     on t.randomid = trand.id
where @ids IS NULL OR t.id IN (select item from dbo.ufnSplit(@ids,','));

因此,如果它是NULL,它将返回所有内容,否则它将计算WHERE子句的OR部分。

您可能需要编辑函数:dbo.ufnSplit来优雅地处理NULL输入,这样才能正常工作。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36006363

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档