当前位置:  开发笔记 > 编程语言 > 正文

where子句中case子句的用法

如何解决《where子句中case子句的用法》经验,为你挑选了1个好方法。

我需要根据下拉值将过滤器添加到存储过程

可用值为0,1,2,3,4,5.@TescilDurum是我的参数.

0 = ALL
1 = TescilDurumId IN 4 OR 5
2 = t.ID IS NULL
3 = dncts.RegistrationCode IS NULL
4 = hsm.ID IS NULL AND dncts.RegistrationCode IS NULL IS NOT NULL
5 = hsm.ENSMRN IS NULL

到目前为止,我从互联网上的类似问题答案中尝试了这个,但我不确定这是否正确.我不确定这种"反向逻辑"是否是正确的方法.而它之后的顺序也给出了错误......

AND 1 =
    CASE WHEN @TescilDurum = 1 AND t.TescilDurumId NOT IN (4,5) THEN 0 
    ELSE CASE WHEN @TescilDurum = 2 AND t.Id IS NOT NULL THEN 0
    ELSE CASE WHEN @TescilDurum = 3 AND hsm.EnsMRN IS NOT NULL THEN 0
    ELSE CASE WHEN @TescilDurum = 4 AND hsm.Id IS NOT NULL AND dncts.RegistrationCode IS NULL THEN 0
    ELSE CASE WHEN @TescilDurum = 5 AND hsm.EnsMRN IS NOT NULL THEN 0
    ELSE 1 END

Lukasz Szozd.. 6

你可以简化它:

WHERE 
  (CASE 
       WHEN @TescilDurum = 0 THEN 1
       WHEN @TescilDurum = 1 AND TescilDurumId IN (4,5) THEN 1
       WHEN @TescilDurum = 2 AND t.ID IS NULL THEN 1
       WHEN @TescilDurum = 3 AND dncts.RegistrationCode IS NULL THEN 1
       WHEN @TescilDurum = 4 AND hsm.ID IS NULL AND dncts.RegistrationCode IS NOT NULL THEN 1
       WHEN @TescilDurum = 5 AND hsm.ENSMRN IS NULL THEN 1
       ELSE NULL    -- this is default and can be skipped if needed
  END) = 1

编辑:

对于任何害怕以上查询总是1 = 1:

CASE有默认的ELSE NULL条款,所以当有整个语句之前不匹配将返回NULLNULL = 1 => NULL.

DECLARE @v INT = 4;

SELECT 1
WHERE(CASE WHEN @v = 1 THEN 1
           WHEN @v = 2 THEN 1
           WHEN @v = 3 THEN 1
           ELSE NULL    -- this is default and can be skipped if needed
      END) = 1
 -- empty resultset

和:

CREATE TABLE #tab(ID INT, col VARCHAR(100));

INSERT INTO #tab
VALUES (1, 'aa'), (2, 'bbb'), (3, NULL), (4, 'aa');

-- DROP DOWN MENU
-- 1 all records
-- 2 only those which ID is in (2,3)
-- 3 only those which has col = 'aa'
-- 4 only those which has NULL value in col

DECLARE @variable INT = 3;

SELECT *
FROM #tab
WHERE 
(
  CASE WHEN @variable = 1 THEN 1
       WHEN @variable = 2 AND ID IN (2,3) THEN 1
       WHEN @variable = 3 AND col = 'aa'  THEN 1
       WHEN @variable = 4 AND col IS NULL THEN 1
       ELSE NULL    -- this is default and can be skipped if needed
 END
) = 1

请记住,存在复合条件,AND因此将返回正确的行.

LiveDemo



1> Lukasz Szozd..:

你可以简化它:

WHERE 
  (CASE 
       WHEN @TescilDurum = 0 THEN 1
       WHEN @TescilDurum = 1 AND TescilDurumId IN (4,5) THEN 1
       WHEN @TescilDurum = 2 AND t.ID IS NULL THEN 1
       WHEN @TescilDurum = 3 AND dncts.RegistrationCode IS NULL THEN 1
       WHEN @TescilDurum = 4 AND hsm.ID IS NULL AND dncts.RegistrationCode IS NOT NULL THEN 1
       WHEN @TescilDurum = 5 AND hsm.ENSMRN IS NULL THEN 1
       ELSE NULL    -- this is default and can be skipped if needed
  END) = 1

编辑:

对于任何害怕以上查询总是1 = 1:

CASE有默认的ELSE NULL条款,所以当有整个语句之前不匹配将返回NULLNULL = 1 => NULL.

DECLARE @v INT = 4;

SELECT 1
WHERE(CASE WHEN @v = 1 THEN 1
           WHEN @v = 2 THEN 1
           WHEN @v = 3 THEN 1
           ELSE NULL    -- this is default and can be skipped if needed
      END) = 1
 -- empty resultset

和:

CREATE TABLE #tab(ID INT, col VARCHAR(100));

INSERT INTO #tab
VALUES (1, 'aa'), (2, 'bbb'), (3, NULL), (4, 'aa');

-- DROP DOWN MENU
-- 1 all records
-- 2 only those which ID is in (2,3)
-- 3 only those which has col = 'aa'
-- 4 only those which has NULL value in col

DECLARE @variable INT = 3;

SELECT *
FROM #tab
WHERE 
(
  CASE WHEN @variable = 1 THEN 1
       WHEN @variable = 2 AND ID IN (2,3) THEN 1
       WHEN @variable = 3 AND col = 'aa'  THEN 1
       WHEN @variable = 4 AND col IS NULL THEN 1
       ELSE NULL    -- this is default and can be skipped if needed
 END
) = 1

请记住,存在复合条件,AND因此将返回正确的行.

LiveDemo

推荐阅读
小妖694_807
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有