Я с тобой.Использование строки через запятую не является плохим подходом.Но вы можете создать Table-Valued Function
для создания таблицы на лету и объединить ее с вашей целевой таблицей.
Create Function [dbo].[Split]
(
@Array nvarchar(4000),
@Separator char,
@ToLower bit = 0
)
Returns
@Result Table
(
ItemKey int Identity(1, 1) Not Null,
ItemValue nvarchar(256) NULL
)
AS
BEGIN
Declare @Index int,
@Value nvarchar(256)
Select @Index = 0
Select @Value = Null
While (1 = 1)
Begin
Select @Index = CharIndex(@Separator, @Array)
If (@Index = 0)
Begin
Insert Into @Result Values (LTRIM(RTRIM(Case @ToLower When 1 Then Lower(@Array) Else @Array End)))
Break
End
Select @Value = SubString(@Array, 0, @Index)
Insert Into @Result Values (LTRIM(RTRIM(Case @ToLower When 1 Then Lower(@Value) Else @Value End)))
Select @Array = Right(@Array, Len(@Array) - @Index)
End
Return
END
Select *
from dbo.TargetTable tt, dbo.Split('101, 102, 103', ',', 0) r
Where (tt.PrimaryKey = r.ItemValue)