SQL multi-condition query implementation methods
SQL multi-condition Query
In the future, we will perform multi-condition queries. One is arrangement and combination, and the other is dynamic concatenation of SQL statements.
For example, we need two conditions: one is date @ addDate and the other is @ name.
The first method is
If (@ addDate is not null) and (@ name <> '') select * from table where addDate = @ addDate and name = @ nameelse if (@ addDate is not null) and (@ name = '') select * from table where addDate = @ addDate else if (@ addDate is null) and (@ name <> '') select * from table where and name = @ nameelse if (@ addDate is null) and (@ name = '') select * from table
The second type is dynamic composition of SQL, which is executed through exec and I will not write it,
Yesterday I thought of a way
Select * from table where (addDate = @ addDate or @ addDate is null) and (name = @ name or @ name = '')
Result 1: debugging is successful,
Please advise if you have any ideas and better methods !~
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!