''andisnotnull)set@sql=@sql+'andname='+@name+''exec(@sql)下面是不采用拼接SQL字符串實現多條件查詢的解決方案第一種寫法是感覺代碼有些冗余if(@addDateisnotnull)an" />

日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

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

系統 2164 0

以前拼接的寫法

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

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。?!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 浏阳市| 威远县| 锡林郭勒盟| 思茅市| 高平市| 社旗县| 鄱阳县| 响水县| 蚌埠市| 五原县| 墨玉县| 肥东县| 宁晋县| 平泉县| 汶川县| 霍邱县| 五华县| 河津市| 和平县| 翼城县| 永吉县| 徐闻县| 班玛县| 筠连县| 民丰县| 金山区| 鄂伦春自治旗| 中西区| 商城县| 安仁县| 白玉县| 云龙县| 陕西省| 北辰区| 阜新市| 黔江区| 神农架林区| 扬中市| 承德县| 望奎县| 阿巴嘎旗|