Thursday, March 31, 2011

SQL ... Select all rows after skipping rows count

I need to select all rows after skipping rows count and here is the select statement for that :

SELECT TOP(@COUNT)
[UserId]
,[LinkId]
,[ShareDate]
,[SharedFromUserId]
,[TotalLike]
,[TotalDislike]
FROM
(
SELECT [t0].*, ROW_NUMBER() OVER (ORDER BY [t0].ShareDate DESC) as RowIndex
FROM UserLink AS [t0]
WHERE [t0].UserId IN ((SELECT UserId FROM UserReader WHERE ReaderId = @USERID) UNION (SELECT @USERID))
) AS [t1]
WHERE [t1].RowIndex > @SKIP
ORDER BY [t1].RowIndex

Using LINQ, You can do it with SKIP and TAKE methods which are another solution. But in my case it was not a good idea to do that as it was a part of a long procedure :)

Hope it helps ...

No comments:

Post a Comment