Question

I have a stored procedure where I am using cross apply to get a field "tagtext" from a table and then put each tagtext together as a entry in a new field called Tags in another table. I'm not to keen on how cross apply works though, and seem to be getting an error: Invalid Object myArticles.

The relevant code from my sp is:

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    declare @RowStart int
    declare @RowEnd int
    IF (@Page=1)
        Begin

            set @RowStart=(@Page-1)*(@PageLen)
            set @RowEnd=(@RowStart+@PageLen);
        END
    ELSE
        BEGIN

            set @RowStart=((@Page-1)*(@PageLen))+1
            set @RowEnd=((@RowStart+@PageLen))-1
        END;
        With myArticles as 
(select ROW_NUMBER() over (ORDER BY publicationdate DESC) as 'RowNumber',*

From article_v
where articleID in(select articleID from savedarticle where userID= @UserID)  and
title like '%'+@keyword1+'%' and
title like '%'+@keyword2+'%' and
title like '%'+@keyword3+'%' and
title like '%'+@keyword4+'%' and
title like '%'+@keyword5+'%' 
)

(select mA.*,
isnull(left(Tags,len(Tags)-1),'') as Tags
from myArticles mA
  cross apply (select tagtext +', '
    from usertag uTag 
    where uTag.userID=@userID and uTag.usertagID in(select usertagID from articletag aTag where aTag.articleID=mA.articleID)
    for xml path('')) ca(Tags)
 )
        select 
            rownumber,journalID,journalname,articleID,title,publicationdate,medabbr, authors, Tags
            from  myArticles where RowNumber Between @RowStart  and @RowEnd
END


GO

Then, the web service I am using the sp in is:

<WebMethod()> _
Public Function GetSavedArticlesWithAbbr(ByVal mobileGUID As String, ByVal pageNum As Integer, ByVal pageLen As Integer, ByVal keywords As String) As List(Of ipadArticle)
    Dim result As New List(Of ipadArticle)
    keywords = HttpUtility.UrlDecode(keywords)
    Dim tempParamKW() As String = {"", "", "", "", ""}
    Dim tempKW() As String = keywords.Split(" ")
    Dim tempILoop As Integer = 0
    For Each s As String In tempKW
        tempParamKW(tempILoop) = s.Trim
        tempILoop += 1
    Next
    Dim simpuser As SimpleUser = utils.GetSimpleUserInfoFromMobileGUID(mobileGUID)
    If simpuser.isValid Then
        Dim lq As New lqDFDataContext
        Dim var = lq.mobile_GetSavedArticlesJW(simpuser.UserID, tempParamKW(0), tempParamKW(1), tempParamKW(2), tempParamKW(3), tempParamKW(4), pageNum, pageLen)
        For Each a_var In var
            Dim ipadartcicle As New ipadArticle()
            ipadartcicle.ArticleID = a_var.articleID
            ipadartcicle.PublishedOn = a_var.publicationdate
            ipadartcicle.Title = a_var.title
            ipadartcicle.MedAbbr = a_var.medabbr.Replace(" ", "-").ToLower()

            Dim tempTags() As String = a_var.Tags.Split(",")
            For Each t As String In tempTags
            ipadartcicle.Tags.Add(t)
            Next

            Dim tempAuthors() As String = a_var.Authors.Split(",")
            For Each a As String In tempAuthors
            ipadartcicle.Authors.Add(a)
            Next

            result.Add(ipadartcicle)
        Next     'this is where i get the error
    End If
    Return result
End Function
Was it helpful?

Solution

I'd say that you wanted to chain another cte between closing and opening parenthesis in following code but forgot about it:

title like '%'+@keyword5+'%' 
)

(select mA.*,

This should probably be

title like '%'+@keyword5+'%' 
),
myArticles2 as (select mA.*,

And final part of query should then use myArticles2. As it is written now, last select is not a part of cte query.

OTHER TIPS

try this, it's like you missed the second part

With myArticles as 
(select ROW_NUMBER() over (ORDER BY publicationdate DESC) as 'RowNumber',*

From article_v
---rest of query here
),
myArticles2 as
(
 select mA.*,
 isnull(left(Tags,len(Tags)-1),'') as Tags
 from myArticles mA
  cross apply (select tagtext +', '
   --- rest of query here
)
   select 
            rownumber,journalID,journalname, .... rest of query here
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top