Skip to content

Instantly share code, notes, and snippets.

@jvinceso
Created June 28, 2016 14:43
Show Gist options
  • Save jvinceso/1380ff81d9c4c93a1c02d86bce08f080 to your computer and use it in GitHub Desktop.
Save jvinceso/1380ff81d9c4c93a1c02d86bce08f080 to your computer and use it in GitHub Desktop.
Paginado de resultado de forma optima en SQL SERVER .. hasta el momento la mejor forma disponible
DECLARE @Numero_Pagina INT = 2, @Filas_x_Pagina INT = 5
DECLARE @vDepNombre VARCHAR(500) = null, @iDepCodigoPadre INT = NULL, @Cantidad_Registros INT
SELECT iDepCodigo,vDepNombre,cDepEstado,iDepCodigoPadre
FROM gen.DEPENDENCIA AS U
--INNER JOIN XXXX AS MU ON U.CODX = MU.CODX
--INNER JOIN YYYYYY AS CU ON CU.CODY = U.CODY
--AND u.cDepEstado = 'A'
WHERE
(@vDepNombre is null OR U.vDepNombre like CONCAT('%',@vDepNombre,'%') )
AND
(@iDepCodigoPadre is null OR U.iDepCodigoPadre like CONCAT('%',@iDepCodigoPadre,'%') )
ORDER BY U.iDepCodigo
OFFSET ((@Numero_Pagina - 1) * @Filas_x_Pagina) ROWS
FETCH NEXT @Filas_x_Pagina ROWS ONLY
Select @Cantidad_Registros = count(*) from gen.DEPENDENCIA AS U
--INNER JOIN XXXX AS MU ON U.CODX = MU.CODX
--INNER JOIN YYYYYY AS CU ON CU.CODY = U.CODY
--AND u.cDepEstado = 'A'
WHERE
(@vDepNombre is null OR U.vDepNombre like CONCAT('%',@vDepNombre,'%') )
AND
(@iDepCodigoPadre is null OR U.iDepCodigoPadre like CONCAT('%',@iDepCodigoPadre,'%') )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment