A simple way of SQL multi-condition query

xiaoxiao2021-03-06  73

In the future, we do more inquiries, one is to arrange the combination, the other is dynamic splicing SQL, such as: we have two conditions, one date @adddate, one is @Name first way to write is

IF (@adddate is not null) and (@Name <> ') select * from table where addddate = @adddate and name = @NameElse if (@adddate is not null) and (@Name =') Select * from Table where addddate = @adddate else if (@adddate is null) and (@Name <> ') select * from table where and name = @NameElse if (@adddate is null) and (@Name =') SELECT * The second type of from Table is dynamically constitutive SQL. I don't write through Exec. I don't write. Yesterday I thought of a way Select * from table where (addddate = @adddate or @adddate is null) and (name = @name @name @ Name = '') Result 1 debug, success, a little idea, there is a better way, please advise! ~

转载请注明原文地址:https://www.9cbs.com/read-93042.html

New Post(0)