CREATE PROCEDURE SP_CommonPageList
@Fields VARCHAR(500),
@From VARCHAR(1000),
@Condition VARCHAR(1000),
@SortBy VARCHAR(500),
@PageIndex NVARCHAR(10),
@PageSize NVARCHAR(10),
@TotalCount INT OUT
AS
DECLARE @SQL NVARCHAR(2000)
SET @SQL ='SELECT @TotalCount=COUNT(1) '+@From+' '+@Condition+' '
EXEC SP_EXECUTESQL @SQL,N'@TotalCount INT OUTPUT', @TotalCount OUTPUT
DECLARE @ExeSQL NVARCHAR(4000)
DECLARE @Count NVARCHAR(10)
DECLARE @LastRow NVARCHAR(10)
SET @Count=CONVERT(INT,@PageSize)*CONVERT(INT,@PageIndex)
SET @LastRow=@TotalCount
PRINT @LastRow
SET @ExeSQL='WITH TempList AS
(SELECT TOP('+@LastRow+') ROW_NUMBER()OVER('+@SortBy+')AS Rows ,'+@Fields+' '+@From+' '+@Condition+')
SELECT TOP ('+@PageSize+') * FROM TempList WHERE Rows>='+@Count+''
--PRINT @ExeSQL
EXEC sp_sqlexec @ExeSQL