Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Row_Number() and T-SQL View

Tags:

t-sql

I am trying to implement paging for a View in T-SQL :

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

For a reason I am getting these errors :

Msg 207, Level 16, State 1, Procedure GetUserUsersFollowing, Line 36
Invalid column name 'RowNumber'. Msg 207, Level 16, State 1, Procedure
GetUserUsersFollowing, Line 36 Invalid column name 'RowNumber'. Msg
207, Level 16, State 1, Procedure GetUserUsersFollowing, Line 34
Invalid column name 'Followers'.

I've used the same code for a table, but I don't know what is happening here. something is missing...

Thanks.

like image 319
Joseph Ghassan Avatar asked Feb 02 '26 14:02

Joseph Ghassan


1 Answers

You're selecting from the table and not the CTE you defined above. You should be doing in your final SELECT, "FROM TH_VW_UserFollowing". I'd also suggest giving your CTE a different name from your table.

like image 150
Kirk Woll Avatar answered Feb 05 '26 06:02

Kirk Woll