SQL Server-正确的语法是什么?
我写的语法正确吗 伪码SQL Server-正确的语法是什么?,sql,if-statement,syntax,where,Sql,If Statement,Syntax,Where,我写的语法正确吗 伪码 select * from products if @value is not null begin where category = @value end + if @value1 is not null begin where other1 = @value1 end + if @value2 is not null begin where other2 = @value2 end + if @value3 is not null begin where other3
select * from products
if @value is not null begin where category = @value end
+ if @value1 is not null begin where other1 = @value1 end
+ if @value2 is not null begin where other2 = @value2 end
+ if @value3 is not null begin where other3 = @value3 end
我是诺布。我不想写动态查询。如何编写上述查询?以下是不使用动态SQL的方法
select *
from products
where (@value is null or category = @value)
and (@value1 is null or other1 = @value1)
and (@value2 is null or other2 = @value2)
and (@value3 is null or other3 = @value3)
它是如何工作的?
取此行@value为空或category=@value
上述条件检查@value是否为null。如果为null,则整行/条件的计算结果为true。因此,我们忽略了其中的或部分
这同样适用于所有其他条件
希望这能说明问题 下面是不使用动态SQL的方法
select *
from products
where (@value is null or category = @value)
and (@value1 is null or other1 = @value1)
and (@value2 is null or other2 = @value2)
and (@value3 is null or other3 = @value3)
它是如何工作的?
取此行@value为空或category=@value
上述条件检查@value是否为null。如果为null,则整行/条件的计算结果为true。因此,我们忽略了其中的或部分
这同样适用于所有其他条件
希望这能说明问题 这是打字错误吗?
ifs
中的@value
应该是@value1
、@value2
等吗?这是打字错误吗?ifs
中的@value
是否应为@value1
、@value2
等?