3

我想使用一个包含比较运算符(如'=', )的参数'>''>='并将其用于带有“case when then”或 if 语句的 where 子句中,如下所示:

WHERE  
case @Operator
 when '=' then (@Amount is null) or (@Amount = 0 ) or (Amount= @Amount)  
 when '>' then (@Amount is null) or (@Amount = 0 ) or (Amount> @Amount)  
 when '>=' then (@Amount is null) or (@Amount = 0 ) or (Amount>= @Amount)      
END
4

2 回答 2

1

我相信这会成功:

WHERE 
  @Amount IS NULL
  OR @Amount = 0
  OR Amount =  CASE @Operator WHEN '='  THEN @Amount END
  OR Amount >  CASE @Operator WHEN '>'  THEN @Amount END
  OR Amount >= CASE @Operator WHEN '>=' THEN @Amount END;
于 2012-09-23T16:04:47.803 回答
0
where isnull(@Amount, 0) = 0
   or ( Amount = @Amount and @Operator = '=')
   or ( Amount > @Amount and @Operator = '>')
   or ( Amount >= @Amount and @Operator = '>=')
于 2012-09-25T11:28:45.187 回答