Hi...
here is a way to write query for custom paging
here is a way to write query for custom paging
create procedure GetCustomersByPage @PageSize int, @PageNumber int, @WhereString varchar(max), @SortExpression varchar(200), @TotalCount int output as begin declare @FirstRecord int declare @LastRecord int set @FirstRecord = (((@PageNumber - 1) * @PageSize) + 1) set @LastRecord = (@PageNumber * @PageSize) declare @sql nvarchar(max) DECLARE @ParmDefinition NVARCHAR(500) SET @ParmDefinition=N'@TotalCount int OUTPUT' set @sql = 'select @TotalCount = count(*) from dbo.Customers where 1=1 '+@WhereString+'; with PagedCustomers as (select row_number() over(order by '+@SortExpression+') as RowNumber, * from dbo.Customers where 1=1 '+@WhereString+') SELECT * from PagedCustomers where RowNumber between '+cast(@FirstRecord as varchar)+'and '+cast(@LastRecord as varchar) exec sp_executesql @sql, @ParmDefinition, @TotalCount=@TotalCount output return @TotalCount end
No comments:
Post a Comment