我的存储过程收到“@CatergoryIDs varchar”
@CatergoryIDs包含由“|”,
分隔的单个或多个ID我的存储过程看起来像这样:
Select R.ID,,CategoryID, (sum(C.Rating)/Count(R.ID)) as "Rating"
from Table1 R
left outer join Table2 C
ON R.ID = C.ID
GROUP BY R.ID
having R.CategoryID = @CatergoryID
RETURN
答案 0 :(得分:2)
您应该可以使用CHARINDEX
。但是,效率非常低,最好将@CategoryID
作为表变量。
SET @CatergoryID = '|'+@CatergoryID+'|';
SELECT R.ID, R.CategoryID, (SUM(C.Rating)/COUNT(R.ID)) AS Rating
FROM Table1 AS R
LEFT OUTER JOIN Table2 AS C
ON R.ID = C.ID
WHERE CHARINDEX('|'+R.CategoryID+'|', @CatergoryID)>0
GROUP BY R.ID, R.CategoryID