WHERE语句中的CASE语句(等于和不等于)

时间:2018-08-08 11:06:59

标签: sql sql-server tsql

我有以下查询,其中我试图根据位变量的值从表中返回不同的值。

有没有一种方法可以代替where条件使之起作用?

DECLARE @isAggregate bit = 0

SELECT 
    *
FROM
    Fields
WHERE
    FieldType  
    CASE 
        WHEN @isAggregate = 1 THEN = 'Aggregate'
        WHEN @isAggregate = 0 THEN <> 'Aggregate'
    END

1 个答案:

答案 0 :(得分:3)

您可以使用布尔逻辑,情况expression不能以这种方式工作:

where (@isAggregate = 1 and FieldType = 'Aggregate') or
      (@isAggregate = 0 and FieldType <> 'Aggregate')