0

我有一个存储过程,我使用交叉应用从表中获取字段“tagtext”,然后将每个 tagtext 作为条目放在另一个表中名为 Tags 的新字段中。不过,我并不热衷于交叉应用的工作原理,而且似乎遇到了一个错误:无效的对象 myArticles。

我的 sp 中的相关代码是:

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

然后,我在其中使用 sp 的 Web 服务是:

<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
4

2 回答 2

3

试试这个,就像你错过了第二部分

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
于 2012-08-09T00:34:56.350 回答
2

我想说您想在以下代码中的右括号和左括号之间链接另一个 cte,但忘记了:

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

(select mA.*,

这应该是

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

然后查询的最后部分应该使用 myArticles2。正如现在所写,最后一个选择不是 cte 查询的一部分。

于 2012-08-09T00:34:34.343 回答