[英]Greater than in SQL CASE statement
我很难找到比SQL语句更大的内容。
这是我的代码:
select one, two three from orders
where case when @orderid > 0 then orders.orderid = @orderid end
@orderid是传递给存储过程的参数。 我们的想法是,如果传递了一个有效的(> 0)orderid,那么将其用作where子句中的过滤器,否则不要全部使用它。
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
CASE总是必须返回一些东西,所以如果你想有条件地“禁用”WHERE子句中的一个语句而不能使用OR,你可以设置等于它自己的东西,并且应该总是为真(除非比较空值) )。
编辑:我还应该说,对于这样的查询,其中可以返回的行数可能会有很大差异(一行对整个表),使用OPTION(RECOMPILE)提示可能有助于单行案例中的性能。
NYCDotnet回答,就像其他答案一样有效,但它们可能不是SARGable
要进行此非SARGable查询..
select one, two three from orders
where
case
when @orderid > 0 then orders.orderid
else 0
end = @orderid
..SARGable,改为:
select one, two three from orders
where
@orderid > 0 and orders.orderid = @orderid
or not (@orderid > 0)
如果@orderid不会变为负数,那么只需简化解决方案:
select one, two three from orders
where
@orderid > 0 and orders.orderid = @orderid
or @orderid = 0
或者更好,即如果@orderid不会变为负数:
select one, two three from orders
where
@orderid = 0
or orders.orderid = @orderid
您不需要在这里使用CASE
。 相同的逻辑,重新安排一点:
select one, two three from orders
where @orderid is null or @orderid <= 0 or orders.orderid = @orderid
您不能将条件用作case
表达式中的值。
您可以使用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
事实上,因为id是一个int,如果它是一个标识列,它将始终大于零,所以你甚至不需要费心检查@orderid是否大于零。 也就是说,上面的代码将使查询无法使用无效的订单ID运行。
你也可以这样做
SELECTone, two, three
FROM orders
WHERE orderid = @orderid AND @orderid > 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.