国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > 綜合 > 正文

SQLServer 存儲過程中不拼接SQL字符串實現多條件查詢

2024-07-21 02:45:16
字體:
來源:轉載
供稿:網友

以前拼接的寫法

set @sql=' select * from table where 1=1 '

if (@addDate is not null)

     set @sql = @sql+' and  addDate = '+ @addDate + '  '
if (@name <>'' and is not null)
    set @sql = @sql+ ' and name = ' + @name + ' '
exec(@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


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 都兰县| 佛学| 乐亭县| 安多县| 高雄县| 金寨县| 黄梅县| 应城市| 岳阳县| 涿州市| 梅河口市| 巩义市| 登封市| 洮南市| 孝义市| 深州市| 涟水县| 石楼县| 隆子县| 广水市| 新河县| 汕头市| 睢宁县| 宁陕县| 多伦县| 永仁县| 宿迁市| 孝感市| 日照市| 华池县| 阳西县| 贺州市| 岑溪市| 永安市| 襄垣县| 华容县| 滨州市| 桐柏县| 安达市| 绿春县| 青海省|