日期:2014-05-18  浏览次数:20469 次

存储过程传入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+ ') ')