开发者

Make an stored procedure using sql server 2005 that allows paging

I have the following SQL Query:

DECLARE @Page int, @RegistriesPerPage int , @FirstRegistry int, @LastRegistry int
Set @Page=1
Set @RegistriesPerPage=2
Set @FirstRegistry = (@Page - 1) * @RegistriesPerPage
Set @LastRegistry = (@Page * @RegistriesPerPage +1)


SELECT I.ID, Name, ROW_NUMBER() OVER (ORDER BY ID DESC) as Counter
FROM Items I
WHERE Counter > @FirstRegistry AND Counter < @LastRegistry

group by Id,Name

Im trying开发者_如何学C to make it work, but aparently i cannot cause i cannot use "Counter" on the where clause (even the ROW_Number().

How i could create stored procedure that gets two parameters (number of items per page, and page to return)?


You can use derived query :

SELECT * FROM 
(
  SELECT I.ID, Name, ROW_NUMBER() OVER (ORDER BY ID DESC) as Counter
  FROM Items I      
  group by Id,Name
)a
WHERE Counter > @FirstRegistry AND Counter < @LastRegistry
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜