存储过程中的obj名称无效

时间:2012-08-09 00:16:51

标签: .net sql-server vb.net stored-procedures cross-apply

我有一个存储过程,我使用交叉应用从表中获取字段“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

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:2)

我想说你想在下面的代码中在结束括号和左括号之间链接另一个cte但是忘了它:

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

(select mA.*,

这应该是

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

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