首页 新闻 会员 周边

sql 查询传参数的问题

0
[已解决问题] 解决于 2011-11-10 09:57

select top(10) PaperId,CreationTime,QuestionTypeId from TB_Paper where QuestionTypeId=3 and PaperDegree ='3' and KnowledgeTypeId in(2,3,4)union all
select top(10) PaperId,CreationTime,QuestionTypeId from TB_Paper where QuestionTypeId=4 and PaperDegree ='4' and KnowledgeTypeId in(2,3,4)union all
select 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 all
select 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 all
select top(10) PaperId,CreationTime,QuestionTypeId from TB_Paper where QuestionTypeId=4 and PaperDegree ='4' and KnowledgeTypeId in(2,3,4)union all
select 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
提问于:2010-12-29 11:14
< >
分享
最佳答案
0

如果在存储过程中使用,可以对传入的参数使用charindex和substring函数来配合分割参数,再执行随后的内容

Localhost | 菜鸟二级 |园豆:443 | 2010-12-29 16:16
其他回答(2)
0

把参数分割 用while语句

 看下 sp_executesql   的用法

死白的man | 园豆:2135 (老鸟四级) | 2010-12-29 12:16
0

我觉得是不是该在调用存储过程之前就把参数确定下来呢?首先程序处理字符串应该比较简单而且比让sql去处理效率来的要高吧

何苦上青天 | 园豆:250 (菜鸟二级) | 2010-12-29 12:50
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册