简体   繁体   中英

Greater than in SQL CASE statement

I'm having a hard time figuring out a greater than in a SQL statement.

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. 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.

Guffa has the right answer, but the way you'd do this using the CASE trick (which does occasionally come in handy) is this:

--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).

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.

NYCDotnet answer, like the rest of the answers here works, but they may not be SARGable

To make this non-SARGable query..

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

..SARGable, do this instead:

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:

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:

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

     or orders.orderid = @orderid

You don't need to use CASE here. 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.

You can just use the or operator to make the condition unused for invalid values:

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. That being said the above code will keep the query from running with an invalid order id.

You could also do it this way

SELECTone, two, three

FROM orders

WHERE orderid = @orderid AND @orderid > 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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