当前位置: 代码迷 >> Sql Server >> 存储过程传入varchar型参数用于查询中in 的有关问题
  详细解决方案

存储过程传入varchar型参数用于查询中in 的有关问题

热度:63   发布时间:2016-04-27 16:21:04.0
存储过程传入varchar型参数用于查询中in 的问题
[email protected]   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 ( '[email protected]+ ') ')
  相关解决方案