有人可以帮我解决这个SQL 08查询吗?运行时出错

时间:2009-10-01 07:21:12

标签: sql sql-server sql-server-2008

我遇到以下查询时遇到问题,该查询返回以下错误:

'文章'不是公认的选择。 关键字“ON”

附近的语法不正确

你有什么明显的错误可以指出吗?

(
@PageIndex  int,
@PageSize   int
)

WITH Article AS
(
SELECT
tbrm_Article.ArticleID, 
tbrm_Article.CountryID,
tbrm_Article.CategoryID,
tbrm_Article.Title,
tbrm_Article.ArticleDetail,
tbrm_Article.Source,
tbrm_Article.ArticleDateTimeAdded,
tbrm_Article.ViewCount,
tbrm_Article.CommentCount,
tbrm_CountryList.CountryName AS CountryName,
tbrm_CountryList.CountryImage AS CountryImage,
tbrm_CategoryList.CategoryName AS CategoryName,
tbrm_CategoryList.CategoryImage AS CategoryImage,
aspnet_Users.UserName AS UserName,
Truth,
Relevance,  
ROW_NUMBER() OVER (ORDER BY tbrm_Article.ArticleDateTimeAdded DESC) AS RowRank

FROM

tbrm_Article INNER JOIN
tbrm_CountryList ON tbrm_Article.CountryID = tbrm_CountryList.CountryID INNER JOIN
    tbrm_CategoryList ON tbrm_Article.CategoryID = tbrm_CategoryList.CategoryID INNER JOIN
    aspnet_Users ON tbrm_Article.UserID = aspnet_Users.UserID


CROSS APPLY (
SELECT tbrm_Votes.ArticleID, AVG(tbrm_Votes.Truth), AVG(tbrm_Votes.Relevance)
FROM tbrm_Votes
GROUP BY tbrm_Votes.ArticleID
) AS Votes(ArticleID,Truth,Relevance)
ON tbrm_Article.ArticleID = tbrm_Votes.ArticleID

)  SELECT * FROM Article
  WHERE Article.RowRank > @PageIndex AND RowRank <= (@PageIndex + @PageSize)
  ORDER BY Article.ArticleDateTimeAdded DESC

1 个答案:

答案 0 :(得分:2)

您需要在“WITH”之前添加分号(它必须是批处理中的第一个语句):

确定 - 更新 - 您正试图在存储过程中执行此操作 - 这会更改某些详细信息...

试试这个:

CREATE PROCEDURE dbo.MyProcedure(@PageIndex int, @PageSize int)
AS 
BEGIN    
    WITH Article AS
    (
      SELECT
       tbrm_Articl
    ........  // rest of your statement
END 

这有用吗?

马克