sql 多条件查询问题 在线等
存储过程有5个参数 @condition5 ,@condition4 ,@condition3 ,@condition2, @condition1
当全部为空时
SELECT column1,column2..........FROM table
当@condition1 ,@condition2,@condition3不为空时
SELECT column1,column2..........FROM table
WHERE column1 Like @condition1 AND column2 = @condition2 AND column3 = @condition3
总之当传入参数不为空时 WHERE 条件中就会做判断
请问有什么好方法,
1.不用分各种情况判断 来执行同一个查询语句块(实在很大)
2.不用在存储过程里 传入字符串参数 把整个查询块拼成sql语句 exec(sql)
------解决方案--------------------
SQL code
SELECT column1,column2..........FROM table
WHERE
(@condition1 is null or column1 Like '%'+@condition1+'%')
AND
(@condition2 is null or column2 = @condition2)
AND
(@condition2 is null or column3 = @condition3)