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
精彩评论