写了个存储过程,中间用到了类似这种写法
Select * From User Where ID In('1,2,3')
其中'1,2,3'是从外面传进来的参数,就这样执行报错:'1,2,3'转换为int类型出错,因为ID是int类型的
想了个比较笨的解决方法:思路-循环将传进来的参数'1,2,3'分割并转换为int然后存储到临时表,之后在In里面Select ID这样就不报错了
Declare @ID Nvarchar(500);
Set @ID='4,11064';
Declare @table_UserID Table
(
ID Int
)
While(CHARINDEX(',',@ID)<>0)
Begin
Insert Into @table_UserID(ID) Values(CONVERT(Int,Substring(@ID,1,CHARINDEX(',',@ID)-1)));
Set @ID=STUFF(@ID,1,CHARINDEX(',',@ID),'');
End
Insert Into @table_UserID (ID) Values(CONVERT(Int,@ID));
Select * From @table_UserID
Select * From User Where ID In(Select ID From @table_User)
结果:
欢迎指正