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

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

SQLServer 存儲過程中不拼接SQL字符串實現(xiàn)多條

系統(tǒng) 2142 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字符串實現(xiàn)多條件查詢的解決方案

第一種寫法是 感覺代碼有些冗余
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字符串實現(xiàn)多條件查詢


更多文章、技術交流、商務合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

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

發(fā)表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 临夏市| 柯坪县| 姚安县| 永福县| 嘉兴市| 平山县| 芜湖市| 东乌珠穆沁旗| 康平县| 新巴尔虎左旗| 城步| 景谷| 木兰县| 璧山县| 永登县| 宝兴县| 黄龙县| 皋兰县| 宁河县| 周口市| 宽甸| 三门县| 黄石市| 汉中市| 二连浩特市| 云阳县| 襄垣县| 五大连池市| 梧州市| 山东省| 星座| 达日县| 治县。| 两当县| 繁峙县| 内丘县| 汨罗市| 伊春市| 扎赉特旗| 仁布县| 荔波县|