I'm creating an e-commerce benchmark website. I have some problems in my GETSITES stored procedure:
Code:
CREATE PROC USER_S_GETSITES
@CategoryID int,
@List int,
@IsPopular bit,
@MaxID int,
@Status bit,
@Word text
AS
SELECT s.*, c.*, AVG(r.Rate) AS RateAVG, Count(r.ID) AS RateCount
FROM Sites AS s
INNER JOIN Rates AS r ON r.SiteID = s.ID
INNER JOIN Categories AS c ON c.ID = s.CategoryID
WHERE (@CategoryID is NULL) OR s.CategoryID = @CategoryID
AND (@MaxID is NULL) OR s.ID < @MaxID
AND (@Status is NULL) OR s.Status = @Status
AND r.Status=1
AND (@Word is NULL) OR s.Name LIKE @Word OR s.Description LIKE @Word
AND (@IsPopular is NULL) OR s.IsPopular=@IsPopular
ORDER BY
CASE @List WHEN 1 THEN s.ID END ASC,
CASE @List WHEN 2 THEN s.ID END DESC,
CASE @List WHEN 3 THEN RateAVG END DESC,
CASE @List WHEN 4 THEN RateCount END DESC
And my problems :
Msg 8120, Level 16, State 1, Procedure USER_S_GETSITES, Line 9
Column 'Sites.ID' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Msg 207, Level 16, State 1, Procedure USER_S_GETSITES, Line 23
Invalid column name 'RateAVG'.
Msg 207, Level 16, State 1, Procedure USER_S_GETSITES, Line 24
Invalid column name 'RateCount'.
I can't solve these problems. What should I do?
ntext,text, andimagedata types will be removed in a future version of SQL Server. Avoid using these data types in new development work, and plan to modify applications that currently use them. Usenvarchar(max),varchar(max), andvarbinary(max)instead. See details here