--先建一个字符串拆分的函数
Create Function f_split(@SourceSql varchar(8000),@StrSeprate varchar(100))
Returns @temp table(col
varchar(100))
As
Begin
Declare
@ch
as
varchar(100)
Set
@SourceSql=@SourceSql+@StrSeprate
While(@SourceSql<>'')
Begin
set
@ch=left(@SourceSql,charindex(',',@SourceSql,1)-1)
insert
@temp
values(@ch)
set
@SourceSql=stuff(@SourceSql,1,charindex(',',@SourceSql,1),'')
End
return
End
--查询包含66,223,4的商品ID集合,你可以改成存储过程或函数,然后你就只需要传入参数就可以了
Declare @A varchar(1000)
Set @A='66,223,4'
Select id集合 From
(
Select id集合,Count(*) As cnt From tab
Cross Apply dbo.f_split(ID集合,',')
Where COL in
(
Select * From dbo.f_split(@A,',')
)
Group By id集合
) S
Where cnt=Len(@A)-Len(Replace(@A,',',''))+1