简体   繁体   English

大于SQL CASE语句

[英]Greater than in SQL CASE statement

I'm having a hard time figuring out a greater than in a SQL statement. 我很难找到比SQL语句更大的内容。

Here's my code: 这是我的代码:

select one, two three from orders
where case when @orderid > 0 then orders.orderid = @orderid end

@orderid is parameter passed to the stored procedure. @orderid是传递给存储过程的参数。 The idea is that if a valid (> 0) orderid is passed, then use that as the filter in the where clause, otherwise don't use it all. 我们的想法是,如果传递了一个有效的(> 0)orderid,那么将其用作where子句中的过滤器,否则不要全部使用它。

Guffa has the right answer, but the way you'd do this using the CASE trick (which does occasionally come in handy) is this: Guffa有正确的答案,但你使用CASE技巧(偶尔派上用场)的方式就是:

--If order ID is greater than 0, use it for selection
--otherwise return all of the orders.
select one, two, three
from orders
where orders.orderid = CASE
    WHEN @orderid > 0 then @orderid
    ELSE orders.orderid
END

The CASE always has to return something, so if you want to "disable" a statement in your WHERE clause conditionally and can't use OR, you can just set the thing equal to itself and that should always be true (except when comparing nulls). CASE总是必须返回一些东西,所以如果你想有条件地“禁用”WHERE子句中的一个语句而不能使用OR,你可以设置等于它自己的东西,并且应该总是为真(除非比较空值) )。

Edit: I should also say that on queries like this where the number of rows that can be returned could vary tremendously (one row versus the whole table), using the OPTION (RECOMPILE) hint may help performance a great deal in the single row case. 编辑:我还应该说,对于这样的查询,其中可以返回的行数可能会有很大差异(一行对整个表),使用OPTION(RECOMPILE)提示可能有助于单行案例中的性能。

NYCDotnet answer, like the rest of the answers here works, but they may not be SARGable NYCDotnet回答,就像其他答案一样有效,但它们可能不是SARGable

To make this non-SARGable query.. 要进行此非SARGable查询..

select one, two three from orders
where 
     case 
     when @orderid > 0 then orders.orderid  
     else 0
     end = @orderid

..SARGable, do this instead: ..SARGable,改为:

select one, two three from orders
where 
     @orderid > 0 and orders.orderid = @orderid

     or not (@orderid > 0)

If @orderid will not ever become negative, just make the solution simpler: 如果@orderid不会变为负数,那么只需简化解决方案:

select one, two three from orders
where 
     @orderid > 0 and orders.orderid = @orderid

     or @orderid = 0

Or better yet, ie if @orderid will not become negative: 或者更好,即如果@orderid不会变为负数:

select one, two three from orders
where 
     @orderid = 0

     or orders.orderid = @orderid

You don't need to use CASE here. 您不需要在这里使用CASE Same logic, re-arranged a little: 相同的逻辑,重新安排一点:

select one, two three from orders
where @orderid is null or @orderid <= 0 or orders.orderid = @orderid

You can't use a condition as a value in the case expression. 您不能将条件用作case表达式中的值。

You can just use the or operator to make the condition unused for invalid values: 您可以使用or运算符使条件不用于无效值:

select one, two, three from orders
where @orderid <= 0 or orders.orderid = @orderid
IF @orderid < 0
   BEGIN
      RETURN
   END

  SELECT one, two, three 
  FROM orders 
  WHERE orders.orderid = @orderid 

In fact since the id is an int and if its an identity column it will always be greater than zero so you do not really even need to bother checking if the @orderid is greater than zero. 事实上,因为id是一个int,如果它是一个标识列,它将始终大于零,所以你甚至不需要费心检查@orderid是否大于零。 That being said the above code will keep the query from running with an invalid order id. 也就是说,上面的代码将使查询无法使用无效的订单ID运行。

You could also do it this way 你也可以这样做

SELECTone, two, three

FROM orders

WHERE orderid = @orderid AND @orderid > 0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM