Tuesday, July 7, 2009

Fixed Records SP

CREATE PROCEDURE [dbo].[Test_Customers_Paginacion]
@PageSize int = 10,
@PageIndex int = 1

AS

DECLARE @RowNumBegin int
DECLARE @RowNumEnd int

-- Index based on "1", so to get the first page
-- we set the param to @PageIndex = 1
SET @RowNumBegin = (@PageIndex -1) * @PageSize
SET @RowNumEnd = @RowNumBegin + @PageSize

-- Make the Quero and add a referente ID to each row by using and auto incremental field
SELECT ContactName, ContactTitle, Address, City, RowNum = Identity (int, 1, 1)
INTO #t
FROM Customers

-- Select the subset of rows that we want to retrieve
SELECT *
FROM #t
WHERE RowNum BETWEEN @RowNumBegin AND @RowNumEnd

-- Just to be sure...
DROP TABLE #t

GO

No comments: