求助:关于where id in(@member_id_array)的写法

hgwyl1 2016-10-23 06:12:39

自己写的语句如下:
@member_id_array nvarchar(max)
select * from 表 where id in (@member_id_array)
——@member_id_array是由前台传入的参量,页面上输出是 19,20 这种形式。
——页面运行不了。

修改sql语句如下
@member_id_array nvarchar(max)
select * from 表 where id in (19,20)
——运行正常

手动执行存储过程...
输入:19,20
——运行正常

网上查了一下,没有找到合适的案例可以直接修改。

我技术比较差,实在改不过来。

请哪位能够帮助给一下语句。
如果方便,update的语句也一起行么……
...全文
107 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
中国风 2016-10-23
  • 打赏
  • 举报
回复
不用动态只能这样用
select * from 表 where ','+@member_id_array+',' LIKE '%,'+RTRIM(id)+',%'
中国风 2016-10-23
  • 打赏
  • 举报
回复
EXEC('select * from 表 where id in ('+@member_id_array+')')
要用动态
卖水果的net 2016-10-23
  • 打赏
  • 举报
回复

-- 使用动态语句 

create table test(id int, num int)
go
insert into test values(18,100),(19,100),(20,100),(21,100)
go
print '原始数据'
select * from test 
go
declare @array varchar(10) ; 
declare @sql varchar(100) ; 
set @array = '19,20'   -- 你的参数集合
set @sql = 'update test set num = 200 where id in (' + @array + ')'
exec (@sql)
go
print '修改后的数据'
select * from test 
go
drop table test 
go

(4 行受影响)
原始数据
id          num
----------- -----------
18          100
19          100
20          100
21          100

(4 行受影响)


(2 行受影响)
修改后的数据
id          num
----------- -----------
18          100
19          200
20          200
21          100

(4 行受影响)


27,579

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 应用实例
社区管理员
  • 应用实例社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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