SQL查询存储过程错误

时间:2014-01-05 12:11:29

标签: asp.net sql sql-server stored-procedures

我正在创建一个电子商务基准网站。我的GETSITES存储过程中存在一些问题:

代码:

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

我的问题:

  

消息8120,级别16,状态1,过程USER_S_GETSITES,第9行
  列'Sites.ID'在选择列表中无效,因为它不包含在聚合函数或GROUP BY子句中   消息207,级别16,状态1,过程USER_S_GETSITES,第23行
  列名称“RateAVG”无效。
  消息207,级别16,状态1,过程USER_S_GETSITES,第24行
  列名称“RateCount”无效。

我无法解决这些问题。我该怎么办?

1 个答案:

答案 0 :(得分:1)

错误1:在Sites.ID条款GROUP BY中包含列group by Sites.ID, ....,如CASE @List WHEN 3 THEN AVG(r.Rate) AS RateAVG END DESC

错误2:CASE @List WHEN 4 THEN Count(r.ID) AS RateCount END DESC

错误3:{{1}}