SQL Server Pagination with Offset And Lmit

CREATE PROCEDURE [dbo].[usp_Contact_GetAllContactList]
    @Offset INT,
    @Limit INT
AS
BEGIN
    DECLARE  @RowTotal     INT
    DECLARE @tblTemp TABLE (
                RowNum INT IDENTITY(1, 1),
                ContactID INT,
                ContactName NVARCHAR(150),
                ContactAddress NVARCHAR(150),
                ContactEmail NVARCHAR(150),
            )

INSERT INTO @tblTemp
        SELECT ContactID ,
        ContactName ,
               ContactAddress ,
               ContactEmail
        FROM  Contact_Table
        ORDER BY
               ContactID DESC
 SELECT @RowTotal = @@ROWCOUNT

SELECT @RowTotal AS RowTotal,
           *
    FROM   @tblTemp
    WHERE  RowNum >= @Offset
           AND RowNum <= (@Offset + @Limit - 1)



Comments