SQL in 查询

wx9288 2016-10-10 01:13:07
一个 查询
select a,b from c where id in(1,2)

ID 是INT
如果要将1,2 变成参数@a然后输入
@a=1,2
select a,b from c where id in(@a)

请问要怎么写,IN 只识别字符串,否则无法执行


...全文
93 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
道素 2016-10-10
  • 打赏
  • 举报
回复
你也可以这样

select a,b from  c where charindex(','+convert(varchar, id)+',', ',1,2,')>0

xxfvba 2016-10-10
  • 打赏
  • 举报
回复
declare @a varchar(100) set @a='1,2' Exec('select a,b from c where id in ('+@a+')')
卖水果的net 版主 2016-10-10
  • 打赏
  • 举报
回复

-- 动态 SQL  

declare  @a varchar(10) ='1,2'
declare @sql varchar(200)
set @sql = 'select a,b from  c where id in(' + @a + ')'
exec(@sql )

34,594

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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