-- Offset starting from 1.
-- This kind of paging returns one extra row
-- such that in mobile apps,
-- we can automatically handle the scroll event
-- in cases where the record contains extra rows
-- For example: If we asked for 10 records and
-- the script returns 11, We can write code to fetch
-- the next 10 records if the last returned list
-- has one extra data.
-- Note : We always need to hide the extra rows that
-- the script returns.
-- Example : If the script returns 11 when 10 was asked,
-- hide the last 11th record.
-- That last row is applicable to trigger the next fetch
-- when the user reaches the end of the page
-- and needs to scroll further
CREATE PROCEDURE [dbo].[usp_SampleSPToSearchUsers]
@offset INT ,
@limit INT ,
@Name NVARCHAR(250)
AS
BEGIN
SELECT * ,
ROW_NUMBER() OVER
( ORDER BY ( [UserID] ) DESC ) AS RowNum
FROM SampleUsersTable
WHERE FirstName + ' ' + LastName LIKE '%' + @Name + '%'
ORDER BY ( [UserID] ) DESC
OFFSET ( @offset - 1 ) ROWS
FETCH NEXT ( @limit + 1 ) ROWS
ONLY;
END;