Arrabi
Arrabi

Reputation: 3768

Select * from Table where (Dynamic condition)

my stored proc receive "@CatergoryIDs varchar"

@CatergoryIDs contains single or multiple IDs separated by "|",

my stored proc look like this :

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

Upvotes: 0

Views: 85

Answers (1)

Lucero
Lucero

Reputation: 60190

You should be able to use CHARINDEX. However, this is very inefficient, it would be better to have @CategoryID as a table variable.

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

Upvotes: 2

Related Questions