where子句中使用不喜欢且不为null的case语句

时间:2018-12-14 14:26:01

标签: sql sql-server where

我正在使用存储过程从现有表中提取数据,该存储过程具有一些是或否选择,用户可以通过复选框在前端进行选择。我想限制他们做出的每一个选择都写一堆不同的If语句。

我的where子句的这一部分有效。该列的数据为YN

Where... and IsSigned = Case When @IncludeSigned = 'Y' then IsSigned else 'N' end

我想在方括号之间添加is not nullnot like的位置。到目前为止,我有

and SignatureType = case when @IncludeElectronic = 'Y' then Type else [NOT like electronic] end

and ReviewDate = Case When @HasReviewDate = 'Y' then [ReviewDate is not null] else null end

3 个答案:

答案 0 :(得分:2)

这可以帮助您使用AND / OR(而不是大小写)

where (ReviewDate is not null or @HasReviewDate = 'Y' ) And (....)

ie,当@HasReviewDate ='Y'查询将返回带有ReviewDate不为null的记录时 当@HasReviewDate!='Y'时,查询将返回ReviewDate为null的记录

答案 1 :(得分:1)

这样想:-

您的第一个案例陈述有两个可能的结果:-

  

IsSigned ='Y'
   IsSigned ='N'

您的后续子句有问题,因为它们在语法上没有意义。因此,第二个是书面回报

  

SignatureType =类型
  SignatureType = [不像电子产品]

和您的第三个:

  

ReviewDate = [ReviewDate不为空]
   ReviewDate =空结尾

因此,运算符必须位于case语句之前,并应用于case语句的所有结果。

例如

  

在myfield不喜欢CASE的情况下,thatfield = 1则是'Fish'ELSE'Chips END

要么产生要么

  

myfield不喜欢“鱼”

     

myfield不喜欢“芯片”

答案 2 :(得分:0)

我相信您不能以这种方式使用,此外,查询内部所产生的影响可能很大,我的建议会改变您使用的策略。