通用分页存储过程

以下通用分页存储过程是根据网上相关资料改写而成,支持各个字段的排序,并且优化了查询速度

create PROCEDURE [dbo].[Paging_RowCount]
@Tables varchar(1000),@PK varchar(100),@Sort varchar(200) = NULL,@sort_type varchar(20) ,
@PageNumber int = 1,@PageSize int = 10,@Fields varchar(1000) = '*',@Filter varchar(1000) = NULL,
@Group varchar(1000) = NULL,@count int output
AS
begin
       
        /*Default Sorting*/
 IF @Sort IS NULL OR @Sort = ''
  SET @Sort = @PK

 /*Find the @PK type*/
 declare @SortTable varchar(100)
 declare @SortName varchar(100)
 declare @strSortColumn varchar(200)
 declare @operator char(2)
 declare @type varchar(100)
 declare @prec int
 
 /*Set sorting variables.*/
 IF charindex('desc',@sort_type)>0
 BEGIN
  set @strSortColumn = @Sort
  set @Sort = @Sort + ' desc '
  set @operator = '<='
 END
 ELSE IF CHARINDEX('asc',@sort_type)>0
 BEGIN
  set @strSortColumn = @Sort
  set @Sort = @Sort + ' asc '
  set @operator = '>='
 END
 ELSE
 BEGIN
  set @operator = '>='
 END
 ------------------------------------------------------------------
 IF CHARINDEX('.', @strSortColumn) > 0
 BEGIN
  set @SortTable = SUBSTRING(@strSortColumn, 0, CHARINDEX('.',@strSortColumn))
  set @SortName = SUBSTRING(@strSortColumn, CHARINDEX('.',@strSortColumn) + 1, LEN(@strSortColumn))
 END
 ELSE
 BEGIN
  set @SortTable = @Tables
  set @SortName = @strSortColumn
 END
 
 DECLARE @strPageSize varchar(50)
 DECLARE @strStartRow varchar(50)
 DECLARE @strFilter varchar(1000)
 DECLARE @strSimpleFilter varchar(1000)
 DECLARE @strGroup varchar(1000)
        declare @begin_id int
 declare @end_id int


 /*Default Page Number*/
 IF @PageNumber < 1
  SET @PageNumber = 1
 
 /*Set paging variables.*/
 set @begin_id = @PageSize * (@PageNumber-1)+1
 set @end_id = @begin_id + @PageSize -1
 
 /*Set filter & group variables.*/

 IF @Filter IS NOT NULL AND @Filter != ''
 BEGIN
  SET @strFilter = ' WHERE ' + @Filter + ' '
  SET @strSimpleFilter = ' AND ' + @Filter + ' '
 END
 ELSE
 BEGIN
  SET @strSimpleFilter = ''
  SET @strFilter = ''
 END
 IF @Group IS NOT NULL AND @Group != ''
  SET @strGroup = ' GROUP BY ' + @Group + ' '
 ELSE
  SET @strGroup = ''
 ---------------------------------------------------------------------------------------------------------------------
       declare @sql nvarchar(4000)
       create table #table (newid int identity(1,1),tid int)
       set @sql = 'insert into  #table(tid) select ' + @PK  + ' from ' + @Tables + @strFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort 
       exec sp_executesql @sql
       set @count=@@rowcount
       set @sql = ' select ' + @Fields + ' from ' + @Tables + ' a join #table b on a.' +@PK + '=b.tid and  b.newid between '
    + convert(varchar(20),@begin_id) + ' and '
                           + convert(varchar(20),@end_id) + '  '  + @strSimpleFilter + ' ' + @strGroup + ' ORDER BY ' + @Sort
        exec sp_executesql @sql
 print @count
     

      

end

 

调用示例:

declare @p12 int
--set @p12=5254
exec  Paging_RowCount 
'laysite','id','site_id','asc',5,10,'ltrim(lon)+''@''+ltrim(lat) as jw,id as
pk,city as ''县市'',antenna_bearing as ''方向角'',sitename as ''站名''','1=1','',@p12 output
select @p12

 

 

 

 

 


 

你可能感兴趣的:(通用分页存储过程)