在存储过程中使用in参数

感觉现在越来越不愿意写东西了,好多时候都是强撑着写,以便以后自己和别人查找。昨天遇到了在存储过程中需要传递in(1,3,9,23)这种类型的参数,一开始我是这么写的,

decalare @ids varchar(8000)

set @ids='1,3,9,23'

select * from 表名 where id in (@ids)

但是执行了一下,提示在将值 '1,3,9,23'转换成数据类型int时失败。

我又不愿意使用动态sql,总觉得效率不高,我们使用存储过程不就是为了提高运行效率吗?然后从网上找了好长时间,终于找到了一个帖子,觉得不错,就转了过来!

declare @ids varchar(max)

set @ids = '113,114,115,116,128,149,1178,8906,11675'

--select * from wko where id in (@ids)

declare @xmlstr xml

set arithabort on

set @xmlstr=convert(xml,'<root><v>' + replace(@ids, ',', '</v><v>') + '</v></root>')

select id = N.v.value('.', 'int')  

into #tmp

FROM @Xmlstr.nodes('/root/v') N(v)

select * from #tmp

原文地址:http://blog.csdn.net/winnyrain/article/details/52934621

猜你喜欢

转载自zhangshufei8001.iteye.com/blog/2380890
今日推荐