views:

48

answers:

1

I am having trouble with the following query which returns the following error:

'Article' is not a recognized option. Incorrect syntax near the keyword 'ON'

Is there something obvious wrong that you can point out??

(
@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
+2  A: 

You need to add a semicolon before your "WITH" (it needs to be the first statement in a batch):

OK - update - you're trying to do this inside a stored procedure - that changes some details...

Try this:

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

Does this work??

Marc

marc_s
Hi Marc. Thanks. Fairly new to all of this so keen to learn. What do you mean about the @pageindex and @pagesize comment?
Cunners
Was this supposed to be a CREATE PROCEDURE (@PageINdex INT, @PageSize INT) or something?
marc_s
thats right. i omitted the first line of the stored proc.
Cunners
btw i now get an error that says, incorrect syntax near';'incorrect syntax near the keyword 'ON'.
Cunners
hi marc. that does indeed work alot better. thanks. i am still getting an error that there is incorrect syntax near the keyword 'ON'. Trying to nut that out now.
Cunners