存储过程中 column(int) in (1,2,3)匹配问题

net_xiaojian 2010-07-13 05:22:06
存储过程中,有的时候需要这样匹配:

select * from tb where id in (1,2,3),但是这个时候1,2,3这个是字符串类型,请问怎么解决。
...全文
82 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
net_xiaojian 2010-07-13
  • 打赏
  • 举报
回复
谢谢两位,用方法一解决。
SQL77 2010-07-13
  • 打赏
  • 举报
回复
select * from tb where ','+@s+',' LIKE '%,'+ltrim(id)+',%'  


弄返了
SQL77 2010-07-13
  • 打赏
  • 举报
回复
--法三
select * from tb where ','+ltrim(id)+',' like '%,'+@s+',%'
SQL77 2010-07-13
  • 打赏
  • 举报
回复
--法三
select * from tb where ','+ltrim(id)+',' like '%,'+@s+',%')>0
htl258_Tony 2010-07-13
  • 打赏
  • 举报
回复

declare @s varchar(100)
set @s='1,2,3'

--法一:
select * from tb where charindex(','+ltrim(id)+',',','+@s+',')>0
--法二:
exec('select * from tb where id in ('+@s+')')

22,210

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧