Sqlserver
 sql >> Base de données >  >> RDS >> Sqlserver

SQL - Récupérer les données page par page

Essayez celui-ci -

CREATE PROCEDURE [dbo].[GetAllContacts] 
(
      @searchVal VARCHAR(500)
    , @from INT
    , @row_count INT = 20
)
AS
BEGIN

    SELECT DISTINCT (Id) AS Id
                ,   FirstName
                ,   LastName
                ,   address
                ,   Tel_no
    FROM tblContact
    WHERE FirstName LIKE '%' + @searchVal + '%'
        OR LastName LIKE '%' + @searchVal + '%'
    ORDER BY LastName
        OFFSET @from ROWS
        FETCH NEXT @row_count ROWS ONLY;

END

Exemple -

EXEC GetAllContacts @searchVal = ''
    ,   @from = 0
    ,   @row_count = 20

EXEC GetAllContacts @searchVal = ''
    ,   @from = 20
    ,   @row_count = 20

EXEC GetAllContacts @searchVal = ''
    ,   @from = 40
    ,   @row_count = 20