在存储过程中使用IN函数,传入多个参数时,会有问题,三种解决方案:
create FUNCTION [dbo].[Split2] ( @RowData varchar(max),@SplitOn varchar(1) ) RETURNS @RtnValue table ( Id int identity(1,1),Data varchar(4000) ) AS BEGIN Declare @Cnt int Set @Cnt = 1 While (Charindex(@SplitOn,@RowData)>0) Begin Insert Into @RtnValue (data) Select Data = ltrim(rtrim(Substring(@RowData,1,Charindex(@SplitOn,@RowData)-1))) Set @RowData = Substring(@RowData,@RowData)+1,len(@RowData)) Set @Cnt = @Cnt + 1 End Insert Into @RtnValue (data) Select Data = ltrim(rtrim(@RowData)) Return END
动态执行sql
EXEC('insert @TempTable select * from mytable where uid in ('+@ids+')')
使用charindex代替,但是此种方案,我没有测试成功,但是网上大部分人说,这个是可以的。所以这个有待商榷。
insert @TempTable select * from mytable where charindex(','+uid+',',''+@ids+'')>0