我正在做一个噩梦,想要在SQL Server 2008中实现以下功能。
如果变量= 1,我将3个变量传入存储过程,然后我需要在where语句中添加子句
我想要做的简单细分就在这里:
SELECT *
FROM products
WHERE categoryId = @categoryId
If @var1 = 1 Then
AND var = @var1
If @var2 = 1 Then
AND var2 = @var2
If @var3 = 1 Then
AND var3 = @var3
ORDER BY productName
答案 0 :(得分:4)
执行此操作的典型方法是将逻辑直接放在where
子句中:
where categoryId = @categoryId and
(@var1 <> 1 or var = @var1) and
(@var2 <> 1 or var2 = @var2) and
(@var3 <> 1 or var3 = @var3)