亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

sql 查詢傳參數的問題

sql 查詢傳參數的問題

慕田峪7331174 2018-12-07 06:50:13
select top(10) PaperId,CreationTime,QuestionTypeId from TB_Paper where QuestionTypeId=3 and PaperDegree ='3' and KnowledgeTypeId in(2,3,4)union allselect top(10) PaperId,CreationTime,QuestionTypeId from TB_Paper where QuestionTypeId=4 and PaperDegree ='4' and KnowledgeTypeId in(2,3,4)union allselect top(10) PaperId,CreationTime,QuestionTypeId from TB_Paper where QuestionTypeId=5 and PaperDegree ='5'? 我的查詢語句是這樣的, 我寫的是一個存儲過程我傳值進去的時候,例如 @QuestionTypeId varchar(50),它的值是'3,4,5',(也有可能是 '4,5,6,7 '),也就是QuestionTypeId 的條件字段,根據 ','來分割,在存儲過程里面應該怎么分割呢?我希望做到的是 如果傳遞的值是 '3,4' 那么 查詢語句就是 select top(10) PaperId,CreationTime,QuestionTypeId from TB_Paper where QuestionTypeId=3 and PaperDegree ='3' and KnowledgeTypeId in(2,3,4)union allselect top(10) PaperId,CreationTime,QuestionTypeId from TB_Paper where QuestionTypeId=4 and PaperDegree ='4' and KnowledgeTypeId in(2,3,4)如果傳遞的值是'3,4,5',那么查詢語句就是select top(10) PaperId,CreationTime,QuestionTypeId from TB_Paper where QuestionTypeId=3 and PaperDegree ='3' and KnowledgeTypeId in(2,3,4)union allselect top(10) PaperId,CreationTime,QuestionTypeId from TB_Paper where QuestionTypeId=4 and PaperDegree ='4' and KnowledgeTypeId in(2,3,4)union allselect top(10) PaperId,CreationTime,QuestionTypeId from TB_Paper where QuestionTypeId=5 and PaperDegree ='5'? 傳遞值具體是不確定的,有可能是 '3,4',也有可能是'3,4,5,6',也有可能是'3,4,5,6,7' 這樣該怎么寫呢?
查看完整描述

3 回答

?
繁星淼淼

TA貢獻1775條經驗 獲得超11個贊

如果在存儲過程中使用,可以對傳入的參數使用charindex和substring函數來配合分割參數,再執行隨后的內容

查看完整回答
反對 回復 2019-01-21
?
慕無忌1623718

TA貢獻1744條經驗 獲得超4個贊

把參數分割 用while語句

?看下 sp_executesql?? 的用法

查看完整回答
反對 回復 2019-01-21
?
慕斯王

TA貢獻1864條經驗 獲得超2個贊

我覺得是不是該在調用存儲過程之前就把參數確定下來呢?首先程序處理字符串應該比較簡單而且比讓sql去處理效率來的要高吧

查看完整回答
反對 回復 2019-01-21
  • 3 回答
  • 0 關注
  • 389 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號