存储过程传入varchar型参数用于查询中in 的问题
我存储过程接收的参数的@id varchar(100)型的,但是实际是一个id的列表
比如 select * from table where id in (@id) //数据库中的id 是int型
传入的是 '1,2,3,4,7,8 ' 结果出错,说:
从数据类型 varchar 转换为 numeric 时出错。
如何解决????
[解决办法]
select * from table where charindex( ', ' + rtrim(id) + ', ', ', ' + @id + ', ') > 0
[解决办法]
改用CharIndex做
Select * From table where CharIndex( ', ' + Cast(id As Varchar) + ', ', ', ' + @id + ', ') > 0
[解决办法]
或者用Like
Select * From table where ', ' + @id + ', ' Like '%, ' + Cast(id As Varchar) + ',% '
[解决办法]
或
exec( 'select * from table where id in ( '+@id+ ') ')