SQLServer 存储过程中不拼接SQL字符串实现多条件查询

下面是 不采用拼接sql字符串实现多条件查询解决方

第一种写法是 感觉代码有些冗余
if (@adddate is not null) and (@name <> '') 
      select * from table where adddate = @adddate and name = @name
else 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 = @name
else if(@adddate is  null) and (@name = '')
select * from table

第二种写法是

select * from table where (adddate = @adddate or @adddate is null) and (name = @name or @name = '')
第三种写法是

select * from table where adddate = case @adddate is null then adddate else @adddate end,name = case @name when '' then name else @name end

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 &#39;EastRiver&#39; 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...