繁体   English   中英

SQL Server如果条件在where子句?

[英]SQL Server If condition at where clause?

我想进行选择查询,我有几个参数,但是根据值,我将参数添加到where子句中。 我不想使用动态查询。 例如,这是我存储的声明:

EXEC GETProducts @ProductID INT = -1, @ProductName NVARCHAR(100) = NULL, @ProductManufacturerID INT = -1

我想要由ManufacturerID = 3制造的所有产品

EXEC GETProducts -1, NULL, 3

我想要这样的查询,我知道它没有用,我也用CASE尝试过,但是没有用。

SELECT * FROM Product
WHERE 
IF(@ProductID > -1)
BEGIN
 ProductID = @ProductID
END
AND
IF(@ProductName <> '')
BEGIN
 ProductName = @ProductName 
END
AND
IF(@ProductManufacturerID > -1)
BEGIN
 ProductManufacturerID = @ProductManufacturerID 
END

谢谢你的帮助!!!

听起来您想要ANDOR的组合,而不是CASEIF

SELECT * FROM Product
WHERE 
(@ProductID <= -1 OR ProductID = @ProductID)
AND
(@ProductName = '' OR ProductName = @ProductName)
AND
(@ProductManufacturerID <= -1 OR ProductManufacturerID = @ProductManufacturerID)

但是我会注意到,在特殊情况下, NULL 通常比“魔术”数字和字符串更可取:

SELECT * FROM Product
WHERE 
(@ProductID IS NULL OR ProductID = @ProductID)
AND
(@ProductName IS NULL OR ProductName = @ProductName)
AND
(@ProductManufacturerID IS NULL OR ProductManufacturerID = @ProductManufacturerID)
SELECT * 
FROM Product
WHERE 
    (@ProductID = -1 OR ProductID = @ProductID)
AND (@ProductName = '' OR ProductName = @ProductName)
AND (@ProductManufacturerID = -1 OR ProductManufacturerID = @ProductManufacturerID)

我想在这里。 您可能想写:

WHERE 
(@ProductID > -1 AND ProductID = @ProductID)
OR
(@ProductName <> '' AND ProductName = @ProductName )
OR
(@ProductManufacturerID > -1 AND ProductManufacturerID = @ProductManufacturerID)

您可以通过将它们与OR子句配对来创建“条件” WHERE子句。 像这样:

SELECT * FROM Product
WHERE
    (@ProductID <= -1 OR ProductID = @ProductID)
    AND @ProductName = '' OR ProductName = @ProductName)
    -- etc.

这样,如果“条件检查”为true,则不会评估OR子句的后半部分。

通过将条件设为复合条件,您可以将查询更改为如下所示,并且不需要CASE表达式。 顺便说一句, IF .. ELSE构造仅在程序主体内起作用。

SELECT * FROM Product
WHERE (@ProductID > -1 AND ProductID = @ProductID)
AND (@ProductName <> '' AND ProductName = @ProductName)
AND (@ProductManufacturerID > -1 AND ProductManufacturerID = @ProductManufacturerID)

您可以在WHERE子句中将条件转换为需求。

IF(@ProductID > -1)
BEGIN
    ProductID = @ProductID
END

等效于:

ProductID > -1 AND ProductID = @ProductID

IF(@ProductName <> '')
BEGIN
    ProductName = @ProductName 
END

等效于:

ProductName <> '' AND ProductName = @ProductName

IF(@ProductManufacturerID > -1)
BEGIN
    ProductManufacturerID = @ProductManufacturerID 
END

等效于:

ProductManufacturerID > -1 AND ProductManufacturerID = @ProductManufacturerID 

因此,您的最终查询将是:

SELECT * FROM Product WHERE 
ProductID > -1 AND
ProductID = @ProductID AND
ProductName <> '' AND
ProductName = @ProductName AND
ProductManufacturerID > -1 AND
ProductManufacturerID = @ProductManufacturerID 

当您的参数打算影响选择查询时,案例将评估为1,因此使其成为select语句的一部分;否则,它将为0,因此将不计数,因为0 = 1将评估为false。

SELECT * FROM Product
WHERE 
(CASE 
     WHEN @ProductId > -1 AND ProductId = @ProductId THEN 1 
     ELSE 0
 END) = 1
AND 
(CASE 
     WHEN @ProductName <> '' AND ProductName = @ProductName THEN 1 
     ELSE 0
END) = 1
AND 
(CASE
     WHEN @ProductManufacturerID > -1 AND ProductManufacturerID = @ProductManufacturerID THEN 1 
END);

暂无
暂无

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

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