Row_Number()和T-SQL视图

时间:2010-09-03 16:48:37

标签: tsql

我正在尝试在T-SQL中实现View的分页:

with TH_VW_UserFollowing  as
(
Select  FollowerID, 
        FollowingID, 
        FollowingFullName, 
        FollowingImage, 
        FollowingUserName,
        dbo.GetUserFollowers(FollowingID) AS Followers,
        ROW_NUMBER() OVER (order by dbo.GetUserFollowers(FollowingID) DESC ) AS 'RowNumber'   
from dbo.TH_VW_UserFollowing 
where FollowerID = @UserID 
)
Select  FollowerID, 
        FollowingID, 
        FollowingFullName, 
        FollowingImage, 
        FollowingUserName, Followers
from dbo.TH_VW_UserFollowing
Where RowNumber BETWEEN @startIdx AND @endIdx

出于某种原因,我收到了这些错误:

  

Msg 207,Level 16,State 1,Procedure GetUserUsersFlowlowing,Line 36
  列名称'RowNumber'无效。消息207,级别16,状态1,程序
  GetUserUsersFollowing,第36行无效的列名称'RowNumber'。消息
  207,Level 16,State 1,Procedure GetUserUsersFlowlowing,Line 34
  列名称“追随者”无效。

我对表使用了相同的代码,但我不知道这里发生了什么。有些东西不见了......

感谢。

2 个答案:

答案 0 :(得分:2)

您是从表格中选择的,而不是您在上面定义的CTE。您应该在最终的SELECT中执行“FROM TH_VW_UserFollowing”。我还建议给你的CTE一个不同的名字。

答案 1 :(得分:1)

为什么要将cte命名为视图名称?