Split 関数を作成して、必要なときに使用できます。
CREATE FUNCTION [dbo].[Split]
(
@List nvarchar(max),
@SplitOn nvarchar(1)
)
RETURNS @RtnValue table (
Id int identity(1,1),
Value nvarchar(max)
)
AS
BEGIN
While (Charindex(@SplitOn,@List)>0)
Begin
Insert Into @RtnValue (value)
Select
Value = ltrim(rtrim(Substring(@List,1,Charindex(@SplitOn,@List)-1)))
Set @List = Substring(@List,Charindex(@SplitOn,@List)+len(@SplitOn),len(@List))
End
Insert Into @RtnValue (Value)
Select Value = ltrim(rtrim(@List))
Return
END
その後、以下のようにクエリで関数を呼び出すことができます。
SELECT * FROM anotherTable WHERE user_id IN(dbo.split(@user_list,','))
それ以外の場合は、動的クエリを使用する必要があります。