SQL Server如果条件在where子句?

时间:2016-01-21 15:36:20

标签: sql sql-server tsql

我想做一个select查询,我有几个参数但是根据值我将参数添加到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

感谢您的帮助!!!

7 个答案:

答案 0 :(得分:2)

听起来您想要ANDOR s而不是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)

答案 1 :(得分:0)

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

答案 2 :(得分:0)

我在这里猜一点。你可能想写:

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

答案 3 :(得分:0)

你可以制作&#34;有条件的&#34; WHERE条款将它们与OR条款配对。像这样:

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

这种方式如果&#34;条件检查&#34;是的,OR子句的后半部分未被评估。

答案 4 :(得分:0)

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

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

答案 5 :(得分:0)

您可以将条件转换为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 

答案 6 :(得分:0)

当你的参数意图影响select查询时,case将评估为1,因此使其成为select语句的一部分,如果不是,它将为0因此它将不计数,因为0 = 1将评估为false。 / p>

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